2013-01-17 22 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> 
相关问题