2013-01-17 20 views
0

我剛開始我的HttpRequest應用程序,我想先設置我的PHP文件。如何檢查我的PHP函數中使用Web瀏覽器是正確的?

我有這個插入功能對我wamp/www/MenuBook/文件夾:

<?php 

// array for JSON response 
$response = array(); 

// check for required fields 
if (isset($_POST['table_ID']) && isset($_POST['MenuBook']) && isset($_POST['order_status'])&& isset($_POST['order_date'])&& isset($_POST['order_receipt'])) { 

    $tableID = $_POST['table_ID']; 
    $menuBook = $_POST['MenuBook']; 
    $order_status = $_POST['order_status']; 
    $order_date = $_POST['order_date']; 
    $order_receipt = $_POST['order_receipt']; 

    // inlude db connect class 
    require_once __DIR__ . '/DBConnect.php'; 

    // connecting to db 
    $db = new DB_CONNECT(); 

    // mysql inserting a new row 
    $result = mysql_query("INSERT INTO orderdb(table_ID,MenuBook,order_status,order_date,order_receipt) VALUES('$table_ID', '$menuBook', '$order_status','$order_date','$order_receipt')"); 

    // check if row inserted or not 
    if ($result) { 
     // successfully inserted into database 
     $response["success"] = 1; 
     $response["message"] = "Order successfully created."; 

     // echoing JSON response 
     echo json_encode($response); 
    } else { 
     // failed to insert row 
     $response["success"] = 0; 
     $response["message"] = "Oops! An error occurred."; 

     // echoing JSON response 
     echo json_encode($response); 
    } 
} else { 
    // required field is missing 
    $response["success"] = 0; 
    $response["message"] = "Required field(s) is missing"; 

    // echoing JSON response 
    echo json_encode($response); 
} 
?> 

現在我想,以檢查它是否會用我的瀏覽器這樣的數據插入到數據庫:

localhost/MenuBook/insertOrder.php?$table_ID=003&$MenuBook=01&$order_status=PENDING&$order_date=2013-01-17&$order_receipt=NO

截至目前它在瀏覽器中返回這個:

{"success":0,"message":"Required field(s) is missing"}

任何想法如何,我可以嘗試,並檢查呢?

回答

2

這將不插入,因爲數據:

  1. 您正在使用$_POST傳遞URL(GET方法)變量,並利用它們在你的腳本。
  2. 另外你還沒有正確地傳遞的變量在URL必須是table_IDMenuBook等代替$table_ID$MenuBook

解決方案是任一用戶$_GET或在腳本$_REQUEST或如果可能的話通過使用POST法(更好的選擇)的值。

來進行測試: 如果你已經在你的腳本中使用$_GET$_REQUEST,只需複製粘貼生成的URL到瀏覽器中,有一個試運行。 否則,如果已經使用POST創建一個示例表(如果你沒有),並提交有一個測試。

0

根據您選擇的瀏覽器,一定會有至少一個REST控制檯插件/擴展程序,它可以輕鬆地測試HTTP請求(包括POST和任何其他方法)並查看其響應。

我用Chrome。其他人喜歡Firefox

curl是另一種選擇,如果你揮動那種方式

1

它看起來像問題是「$」標誌,你必須在URL中。網址變量不需要這些; thy're只是爲了php。嘗試沒有$ s。

+0

返回相同的消息沒有$符號。我使用的網址有問題嗎? –

+1

見上面的Ravi's anser;你已經在你的代碼中使用了$ _POST,當你的變量使用$ _GET傳遞時 - 所以變量永遠不會被腳本拾取。 – ChidG

+0

但是請注意,由於Ravi說POST是一個更好的選擇(特別是如果您使用表單傳遞數據),但是如果您沒有這樣做,您需要構建一個表單來測試它。 – ChidG

1

最簡單的做法是創建一個包含表單的HTML頁面,幷包含所有正確的字段。確保將其提交到正確的路徑,並且該方法是POST。

例如:

<html> 
    <head></head> 
    <body> 
     <form action="http://localhost/MenuBook/insertOrder.php" method="POST"> 
      <input type="text" name="table_ID" /> 
      <input type="text" name="MenuBook" /> 
      <input type="text" name="order_status" /> 
      <input type="text" name="order_date" /> 
      <input type="text" name="order_receipt" /> 
      <input type="submit" /> 
     </form> 
    </body> 
</html> 
相關問題