volcano_Riya volcano_Riya - 2 months ago 20
PHP Question

How to Stop repeat Database in Android?

I m new in android. here i make simple service.which is auto refresh after 5 min. but problem is that database upload repeated every time. So help me.... And in my code single string upload every time but i wont to make

JSONArray
. So help me out how to make
JSONArray
in my code.....

public class One extends Service {
public static final long NOTIFY_INTERVAL = 5*60*1000;
private Handler handler = new Handler();
private Timer timer = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
if(timer != null)
{
timer.cancel();
}
else
{
timer = new Timer();
}
timer.scheduleAtFixedRate(new DisplayTime(),0,NOTIFY_INTERVAL);
}

class DisplayTime extends TimerTask
{

@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
recll();
Toast.makeText(getApplicationContext(),"run",Toast.LENGTH_SHORT).show();
}
});
}
private void recll() {
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
final String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
final String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
final String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString();
Date date1 = new Date(Long.valueOf(date));
final String type = cursor.getString(cursor.getColumnIndexOrThrow("type")).toString();

final String fDate = date1.toString();

cursor.moveToNext();

class getSMSDetails extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... params)
{
HashMap<String,String> param = new HashMap<String, String>();
param.put(Connect.KEY_NUMBER,number);
param.put(Connect.KEY_TYPE,type);
param.put(Connect.KEY_DATE,fDate);
param.put(Connect.KEY_BODY,body);

RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Connect.URL_ADD, param);
return res;
}
}

getSMSDetails idata = new getSMSDetails();
idata.execute();
}
}
cursor.close();
}
}

@Override
public void onDestroy() {
super.onDestroy();
}
}


here is my PHP code.....here i have requirement of enter 7 days data from entry time and whenever new entry comes i want enter new entry while it enters whole data again so i want to prevent duplicate entry thanks in advance....

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$number = $_POST['number'];
$type = $_POST['type'];
$dates = $_POST['date'];
$content = $_POST['content'];
$start = strtotime("-7 days");
$date = strtotime($dates);
$end = strtotime("now");

$query=mysqli_query($con,"SELECT * FROM message_detail where number = '$number' and type = '$type' and date = '$date' and content = '$content'");

if(mysqli_num_rows($query)>0)
{
echo "already exist";
}
elseif($start <= $date && $end >= $date)
{
//Creating an sql query
$sql = "INSERT IGNORE INTO message_detail (number,type,date,content) VALUES ('$number','$type','$date','$content')";
}
//Importing our db connection script
require_once('connect.php');

//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Entry Added Successfully';
}else{
echo 'Could Not Add Entry';
}
//Closing the database
mysqli_close($con);
}


connection.php

<?php

//Defining Constants
define('HOST','mysql.hostinger.in');
define('USER','u336100496_hiren');
define('PASS','');
define('DB','u336100496_sebu');

//Connecting to Database
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>

Answer

Move your connection require to the top of your script.

Where it was you were executing the first query without a connection so it would always fails and therefore take the ELSE that inserts a new row

Also you are running the INSERT in the wrong place it should be in the ELSEIF

<?php 
//Importing our db connection script
require_once('connect.php');

if($_SERVER['REQUEST_METHOD']=='POST'){
   //Getting values
    $number = $_POST['number'];
    $type = $_POST['type'];
    $dates = $_POST['date'];
    $content = $_POST['content'];
    $start = strtotime("-7 days");
    $date = strtotime($dates);
    $end = strtotime("now");

    $query=mysqli_query($con,"SELECT * 
                              FROM message_detail 
                              where number = '$number' 
                                and type = '$type' 
                                and date = '$date' 
                                and content = '$content'");

    if(mysqli_num_rows($query)>0) {
        echo "already exist";
    }
    elseif($start <= $date &&  $end >= $date) {
        //Creating an sql query
        $sql = "INSERT IGNORE INTO message_detail (number,type,date,content) VALUES   ('$number','$type','$date','$content')";

        //Executing query to database
        if(mysqli_query($con,$sql)){
            echo 'Entry Added Successfully';
        }else{
            echo 'Could Not Add Entry';
        }

    } 

    //Closing the database 
    mysqli_close($con);
}
Comments