0

我刚刚启动了我的 HTTPREQUEST 应用程序,我正在尝试首先设置我的 php 文件。

我的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"}

有什么想法可以尝试检查吗?

4

4 回答 4

2

这不会插入数据,因为:

  1. 您在 URL(GET 方法)中传递变量,并在脚本中使用$_POST.
  2. 此外,您甚至还没有正确传递 URL 中的变量,它必须是table_ID, MenuBooketc 而不是$table_ID, $MenuBook

解决方案是用户$_GET$_REQUEST在您的脚本中,或者如果可能的话,使用方法传递值POST(更好的选择)。

用于测试:如果您已经使用$_GET$_REQUEST在您的脚本中,只需将生成的 URL 复制粘贴到浏览器中并进行测试运行。否则,如果已使用POST创建示例表单(如果您没有)并提交以进行测试。

于 2013-01-17T06:49:47.003 回答
1

看起来问题出在 URL 中的“$”符号上。URL 变量不需要那些;你只是为了PHP。不带 $s 试试看。

于 2013-01-17T06:49:48.120 回答
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>
于 2013-01-17T06:50:30.727 回答
0

根据您选择的浏览器,肯定会有至少一个 REST 控制台插件/扩展,可以轻松测试 HTTP 请求(包括 POST 和任何其他奇特的方法)并查看它们的响应。

我使用。其他喜欢Firefox

curl是另一种选择,如果你那样摆动。

于 2013-01-17T06:47:37.827 回答