将数据插入服务器

时间:2017-09-14 14:40:45

标签: php android

我在将数据插入数据库时​​遇到问题。无法插入数据,并且我的Logcat中没有显示任何错误。我已经开始研究但仍然无法解决问题。这是我的PHP代码:

    <?php

    require ("config1.php");

    if(!empty($_POST)){

        $query = "SELECT * FROM announcement WHERE announceID = :announcementID";
        $query_params=array(':announcementID'=> $_POST['announcementID']);

        try{
            $stmt=$db->prepare($query);
            $stmt->execute($query_params);


        }catch(PDOException $ex){
            $response["success"]=0;
            $response["message"]="Database Error1. Please try again";
            die(json_encode($response));
        }


        $row = $stmt->fetch();
        if($row){

            $query = "INSERT INTO announcement (title,description,start_date,end_date,time)
                                     VALUES (:title,:description,:starDate,:endDate,:time) ";

                    $query_params= array(
                        ':title'=>$_POST['title'];
                        ':description'=>$_POST['description'];
                        ':startDate' => $_POST['start_date'];
                        ':endDate' => $_POST['end_date'];
                        ':time' => $_POST['time'];

                    );


                  try {
                    $stmt = $db->prepare($query);
                    $result = $stmt->execute($query_params);
                }

                catch (PDOException $ex) {
                    $response["success"] = 0;
                    $response["message"] = "Database Error2. Please Try Again!";
                    die(json_encode($response));
                }

                $response["success"] = 1;
                $response["message"] = "Update successful!";
                echo json_encode($response)

          }
      }
    ?>

以下是我的java代码:

protected String doInBackground(Void... params) {
                RequestHandler rh=new RequestHandler();
                HashMap<String,String> param= new HashMap<String, String>();
                param.put(KEY_TITLE,announcement_title);
                param.put(KEY_DESCRIPTION,announcement_desc);
                param.put(KEY_START_DATE,start_date);
                param.put(KEY_END_DATE,end_date);
                param.put(KEY_TIME,time);
                param.put(KEY_IMAGE,announcement_image);
                String result= rh.sendPostRequest(ANNOUNCEMENT_URL,param);
                return result;
            }

欣赏是有人可以指出问题。

1 个答案:

答案 0 :(得分:0)

你的PHP代码有语法错误和Mysql准备占位符错误。

我重写了代码,请用旧代码替换。

    <?php

    require ("config1.php");

    if(!empty($_POST)){
//kindly filter the POST value
        $query = "SELECT * FROM announcement WHERE announcementID = :announcementID";
        $query_params=array(':announcementID'=> $_POST['announcementID']);
            $stmt=$db->prepare($query);
            $stmt->execute($query_params);

/*
        }catch(PDOException $e){
            $response["success"]=0;
            $response["message"]="Database Error1. Please try again";
            die(json_encode($response));
        }
*/

        $row = $stmt->fetch();
        if($row){

            $query = "INSERT INTO announcement (title,description,start_date,end_date,time)
                                     VALUES (:title,:description,:starDate,:endDate,:time)";
//re written by Ajmal PraveeN
                    $query_params= array(
                        ':title'=>$_POST['title'],
                        ':description'=>$_POST['description'],
                        ':startDate' => $_POST['start_date'],
                        ':endDate' => $_POST['end_date'],
                        ':time' => $_POST['time']
                        );

/*
                  try {
*/
                    $stmt = $db->prepare($query);
                    $result = $stmt->execute($query_params);
/*
                }

                catch (PDOException $ex) {
                    $response["success"] = 0;
                    $response["message"] = "Database Error2. Please Try Again!";
                    die(json_encode($response));
                }
*/
                $response["success"] = 1;
                $response["message"] = "Update successful!";
                echo json_encode($response);

          }
      }
?>

如果您的$_POST['announcementID']是数字号码,我可以使用Sanitize和Title帖子重新编辑帖子。