2017-09-14 107 views
0

我有問題將數據插入到我的數據庫中。數據無法插入,並且在我的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

我無法找到你要發送'announcementID'的地方 - 所以我敢打賭一個未定義的索引錯誤,可能會被壓制。 - >確保error_reporting()已打開! – Jeff

回答

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']是一個數字號碼我可以重新編輯了消毒和標題帖子的帖子了。

+0

嗨,謝謝,我能否知道你改寫了什麼?我看不到有什麼不同。 – Kleorence

+0

':announcementID'和$ query_params = array()和echo json_encode($ response);這些都是常見的錯誤:)剛剛糾正..所以代碼開始工作?所以投票.. :)你想過濾或消毒? –

+0

現在顯示「數據庫錯誤1,請重試」。我能知道爲什麼嗎? – Kleorence

相關問題