【问题标题】:passing multi params with URL on php在php上使用URL传递多参数
【发布时间】:2014-01-31 05:15:52
【问题描述】:

我有一个用于在表中创建记录的 .php。我认为这是正确的,但我无法将参数从 URL 传递给它,这有问题。

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

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

// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

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

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product 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);
}
?>

我像这样在 url 中传递参数:

localhost:81/android/create_product.php?name='test'&price='35000'&description='test'

是php代码错了还是其他原因?

【问题讨论】:

    标签: php insert insert-update


    【解决方案1】:

    使用$_GET 作为 URL 参数

    if (isset($_GET['name']) && isset($_GET['price']) && isset($_GET['description'])) 
    

    【讨论】:

      【解决方案2】:

      如果你在 url 中传递参数,那么你需要通过

      $_GET['name']
      

      方法不是 $_POST['name'] 之类的

      if (isset($_GET['name']) && isset($_GET['price']) && isset($_GET['description']))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 2012-09-05
        相关资源
        最近更新 更多