为什么我的PHP代码返回null值?

时间:2013-08-05 12:18:03

标签: java php android

我是新手,但我有一个问题,当我尝试将新值插入数据库时​​,我的PHP代码返回一个空值。

我的php代码:

<?php

require("db_connect.php");

if (!empty($_POST)) {

//initial query

$query = "INSERT INTO examclaims ( Trainee_Name, Department, Trainee_ID, Batch_ID, Exam_ID, Attempts, Exam_Date, Invoice_no, Invoice_Date, BeforeGST, GST, AFTERGST)
 VALUES ( :Trainee_Name, :Department, :Trainee_ID, :Batch_ID, :Exam_ID, :Attempts, :Exam_Date, :Invoice_no, :Invoice_Date, :BeforeGST, :GST, :AFTERGST) ";

//Update query
$query_params = array(
    ':Trainee_Name' => $_POST['traineename'],
    ':Department' => $_POST['departmentname'],
    ':Trainee_ID' => $_POST['traineeid'],
    ':Batch_ID' => $_POST['batchid'],
    ':Exam_ID' => $_POST['examid'],
    ':Attempts' => $_POST['attempted'],
    ':Exam_Date' => $_POST['examdate'],
    ':Invoice_no' => $_POST['invoiceno'],
    ':Invoice_Date' => $_POST['invoicedate'],
    ':BeforeGST' => $_POST['beforegst'],
    ':GST' => $_POST['gst'],
    ':AFTERGST' => $_POST['aftergst'],
);

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

}

catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error. Couldn't add post!";
    die(json_encode($response));
}

$response["success"] = 1;
$response["message"] = "Username Successfully Added!";
echo json_encode($response);
} else {

?>
    <h1>Add Comment</h1>
    <form action="ExamClaims.php" method="post">
        name:<br />
        <input type="text" name="traineename" placeholder="name" />
        <br /><br />
        Department:<br />
        <input type="text" name="departmentname" placeholder="post department" />
        <br /><br />
        Message:<br />
        <input type="text" name="traineeid" placeholder="post trainee id" />
        <br /><br />
        <input type="text" name="batchid" placeholder="post batchid" />
        <br /><br />
        <input type="text" name="examid" placeholder="post examid" />
        <br /><br />
        <input type="text" name="attempted" placeholder="post attempted" />
        <br /><br />
        <input type="text" name="examdate" placeholder="post examdate" />
        <br /><br />
        <input type="text" name="invoiceno" placeholder="post invoiceno" />
        <br /><br />
        <input type="text" name="invoicedate" placeholder="post invoice date" />
        <br /><br />
        <input type="text" name="beforegst" placeholder="post before" />
        <br /><br />
        <input type="text" name="gst" placeholder="post gst" />
        <br/><br/>
         <input type="text" name="aftergst" placeholder="post after" />
        <br /><br />
        <input type="submit" value="Submit" />
    </form>
<?php
}
?>

我的android代码:

class SubmitClaims extends  AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
            String traineeName = traineename.getText().toString();
            String Department = departmentname;
            String Traineeid = traineeid.getText().toString();
            String Batchid = batchid.getText().toString();
            String ExamID = examid.getText().toString();//attempts
            String Attempts = attempted;
            String Exam_date = examdate;
            String Invoice_no =invoiceno.getText().toString();
            String Invoice_date = invoicedate;
            String BGST = beforegst.getText().toString();
            String GST = gst.getText().toString();
            String AGST = aftergst.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("traineename", traineeName));
                params.add(new BasicNameValuePair("departmentname", Department));
                params.add(new BasicNameValuePair("traineeid", Traineeid));
                params.add(new BasicNameValuePair("batchid", Batchid));
                params.add(new BasicNameValuePair("examid",ExamID));//attmempts
                params.add(new BasicNameValuePair("attempted",Attempts));//attmempts
                params.add(new BasicNameValuePair("examdate" ,Exam_date));
                params.add(new BasicNameValuePair("invoiceno", Invoice_no));
                params.add(new BasicNameValuePair("invoicedate", Invoice_date));
                params.add(new BasicNameValuePair("beforegst",  BGST)); 
                params.add(new BasicNameValuePair("gst",  GST)); 
                params.add(new BasicNameValuePair("aftergst",  AGST)); 

               Log.d("request!", "starting");

                JSONObject json = jsonParser.makeHttpRequest(ExamClaims_URL, "POST", params);   

                            // Async json success tag
                            int success = json.getInt(TAG_SUCCESS);
                            if (success == 1) {
                                Log.d("Submitted", json.toString());
                                finish();                                   
                                return json.getString(TAG_MESSAGE);
                            } else {
                                Log.d("Fail to Submit!", json.getString(TAG_MESSAGE));
                                return json.getString(TAG_MESSAGE);
                            }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        return null;
    }   

 }

这段代码有什么问题?请提前帮助和感谢

1 个答案:

答案 0 :(得分:0)

当php代码发送空值时跟踪和错误调试..

我认为你的意思是零?

因为我觉得你的代码在这里破了吗?

catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error. Couldn't add post!";
    die(json_encode($response));
}