2014-01-12 18 views
0

我有一個.php表創建記錄。我認爲這是正確的,但我不能從URL傳遞參數到它和錯誤的東西。傳遞多個參數與URL上的PHP

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

我通過PARAMS的網址是這樣的:

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

是PHP代碼是錯誤的,或者是其他什麼東西?

回答

1

使用$_GET爲URL paramiters

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

如果傳遞的URL參數,那麼你需要通過

$_GET['name'] 

方法[ '名']像

得到它不是由$ _ POST
if (isset($_GET['name']) && isset($_GET['price']) && isset($_GET['description']))