php - 如何在 Android 中停止重复数据库?

标签 php android mysql arrays json

我是 Android 新手。这里我做了一个简单的服务,5分钟后自动刷新。但问题是数据库每次都会重复上传。所以帮助我......在我的代码中每次都会上传单个字符串,但我不会制作 JSONArray。所以帮我看看如何在我的代码中创建 JSONArray......

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();
    }
}

这是我的 PHP 代码.....这里我需要输入从输入时间起 7 天的数据,每当新条目出现时,我想在它再次输入整个数据时输入新条目,所以我想防止重复输入,谢谢前进....

<?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);
}

连接.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');
?>

最佳答案

将连接要求移至脚本顶部。

您在没有连接的情况下执行第一个查询,因此它总是会失败,因此采用插入新行的 ELSE

此外,您在错误的位置运行 INSERT,它应该在 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);
}

关于php - 如何在 Android 中停止重复数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39345931/

相关文章:

php - 如何捕获本地时间并将其存储在mysql表中?

Android:无法清除对 SurfaceView 的初始绘制

mysql - 我可以在 MySQL 触发器中使用类似事务的功能吗

php - WordPress 表单上所需的警报

php - 不小于零 - 将小于零的变量设为零

android - Google Maps API V2 日常使用

java - Arduino ADK 无法通过 USB 从 Galaxy S II 接收数据

mysql - 数据库设计-对象继承

mysql - 计算不同表中特定值的行数,两个表都包含同一列?

php - 将 php 变量值传递给 javascript