如何使用Web浏览器检查我的php功能是否正确?

I have just started my HTTPREQUEST application and I am trying to set up my php files first.

I have this insert function on my wamp/www/MenuBook/ folder :

<?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);
}
?>

Now I would like to check if it would insert data into the database using my browser something like this :

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

as of now it returns this in the browser:

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

Any ideas how I could try and check this?

This will not insert data because:

  1. You are passing variables in URL (GET Method) and using them in your script using $_POST.
  2. Also you have not even correctly passed the variables in URL it must be table_ID, MenuBook etc instead of $table_ID,$MenuBook.

The solution is either user $_GET or $_REQUEST in your script or if possible pass the values using POST method (an better choice).

For testing: If you have used $_GET or $_REQUEST in your script, simply copy-paste the generated URL into the browser and have a test run. Else if have used POST create a sample form(If you don't have) and submit it to have a test.

Depending on your browser of choice, there's bound to be at lease one REST console addon/extension that easily allows you to test HTTP requests (including POSTs and any of the other fancy methods) and see their responses.

I use Chrome. Others like Firefox.

curl is another option, if you swing that way.

It looks like the problem is the '$' signs you have in the URL. URL variables do not need those; thy're just for php. Try it without the $s.

The easiest thing to do is to create an HTML page containing the form, with all the correct fields. Ensure you submit it to the correct path, and that the method is POST.

For example:

<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>