2013-12-10 20 views
0

這是我shipmentrequest.php保持到數據庫錯誤2在我的PHP腳本

<?php 

require("config.inc.php"); 

if (!empty($_POST)) { 

if (empty($_POST['name']) || empty($_POST['mobilenumber']) || empty($_POST['address']) || empty($_POST['city']) || empty($_POST['postcode']) || empty($_POST['state'])) { 

    // Create some data that will be the JSON response 
    $response["success"] = 0; 
    $response["message"] = "Please Enter the required marked ** field."; 

    die(json_encode($response)); 
} 
else if (strlen($_POST['postcode']) < 5 || strlen($_POST['postcode']) >= 6) { 
$response["success"] = 0; 
    $response["message"] = "Your postcode should be only 5 numbers."; 
die(json_encode($response));    
} 

$query = "INSERT INTO shipmentrequest (name, mobilenumber, address, city, postcode, state) VALUES (:receivername, :receivermobilenumber, :receiveraddress, :receivercity, :receiverpostcode, :receiverstate) "; 

//Again, we need to update our tokens with the actual data: 
$query_params = array(
    ':name' => $_POST['name'], 
    ':mobilenumber' => $_POST['mobilenumber'], 
    ':address' => $_POST['address'], 
':city' => $_POST['city'], 
    ':postcode' => $_POST['postcode'], 
':state' => $_POST['state'] 
); 

//time to run our query, and create the user 
try { 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute($query_params); 
} 
catch (PDOException $ex) { 
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage()); 

    //or just use this use this one: 
    $response["success"] = 0; 
    $response["message"] = "Database Error2. Please Try Again!"; 
    die(json_encode($response)); 
} 

//If we have made it this far without dying, we have successfully added 
//a new user to our database. We could do a few things here, such as 
//redirect to the login page. Instead we are going to echo out some 
//json data that will be read by the Android application, which will login 
//the user (or redirect to a different activity, I'm not sure yet..) 
$response["success"] = 1; 
$response["message"] = "Shipping Service Succesfully Requested!"; 
echo json_encode($response); 

//for a php webservice you could do a simple redirect and die. 
//header("Location: login.php"); 
//die("Redirecting to login.php"); 


} else { 
?> 
<h1>Shipment Request</h1> 
<form action="shipmentrequest.php" method="post"> 
    Name:<br /> 
    <input type="text" name="name" value="" /> 
    <br /><br />  
    Mobile Number:<br /> 
    <input type="text" name="mobilenumber" value="" /> 
    <br /><br /> 
    Address:<br /> 
    <input type="text" name="address" value="" /> 
    <br /><br /> 
    City:<br /> 
    <input type="text" name="city" value="" /> 
    <br /><br /> 
    Postcode:<br /> 
    <input type="text" name="postcode" value="" /> 
    <br /><br /> 
    State:<br /> 
    <input type="text" name="state" value="" /> 
    <br /><br /> 
    <input type="submit" value="Request Service" /> 
</form> 
<?php 
} 

?> 

我不知道爲什麼連我都充滿了我的郵政編碼文本框,只有5個號碼,它只是將跳轉到異常處理這是... Database Error2. Please Try Again!任何人都可以告訴我爲什麼它會去catch語句?

+2

您是否嘗試過呼應*實際* 錯誤信息?它可能有更多的信息,而不僅僅是「請再試一次」。在測試時註釋出錯信息並不是真的有幫助。 –

+4

「* die(」無法運行查詢:「。$ ex-> getMessage()); *」。 Sooo ..它輸出了什麼? – h2ooooooo

+0

我只是不知道它爲什麼只是爲了'catch(PDOException $ ex)',即使我已經正確填寫了所有信息 – Nick

回答

1

綁定參數名稱值不匹配。

應該:name, :mobilenumber, etc而不是:receivername, :receivermobilenumber, etc

$query = "INSERT INTO shipmentrequest (name, mobilenumber, address, city, postcode, state) VALUES (:name, :mobilenumber, :address, :city, :postcode, :state) "; 
+0

,但是在我的數據庫變量中是receivername,receivermobilenumber等等。 – Nick

+1

@Nick:這和你的「數據庫變量」無關。這些「變量」就是這樣PDO知道用什麼來取代什麼。 –

2

看起來像$query這些標記不符合你把什麼$query_params陣列匹配:

VALUES (:receivername, :receivermobilenumber, :receiveraddress, :receivercity, :receiverpostcode, :receiverstate) 
相關問題