php文件没有显示错误

时间:2013-10-30 06:46:11

标签: php wamp

我在wamp服务器中有PHP文件。当我在localhost中运行该文件时,会出现如下错误。

这是我的PHP文件:

<?php
$response = array({
    "success": [
        {
            "message": "Required field id missing"
        },
        {
            "message": "successfully created."
        },
        {
            "message": "Oops! An error occured"
        }
    ]}
);
 
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {
 
    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];
 

    require_once __DIR__ . '/db_connect.php';
 
    
    $db = new DB_CONNECT();
 
   
    $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");
 
   
    if ($result) {
        
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";
 
       
        echo json_encode($response);
    } else {
      
        $response["success"] = 2;
        $response["message"] = "Oops! An error occurred.";
 
    
        echo json_encode($response);
    }
} else {
   
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    
    echo json_encode($response);
}
?>

当我在错误显示后在localhost中运行它时,

  

(!)解析错误:语法错误,意外的'{',期待')'in   第2行的D:\ wamp \ www \ android \ create_product.php

还建议我如何设置localpath以在浏览器中运行php文件。

2 个答案:

答案 0 :(得分:1)

您的数组格式错误,您不需要它,因为您在条件中向数组添加消息,所以只需删除此部分:

$response = array({
    "success": [
        {
            "message": "Required field id missing"
        },
        {
            "message": "successfully created."
        },
        {
            "message": "Oops! An error occured"
        }
    ]}
);

答案 1 :(得分:0)

您的代码最好看一下:

  • 你不应该在每个条件语句结束后回应响应,你可以做一次。
  • 使用isset功能时,您可以根据需要使用多个参数。


 isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])

等于:

 isset($_POST['name'],$_POST['price'],$_POST['description'])

最终:

if (isset($_POST['name'],$_POST['price'],$_POST['description'])) {

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


    require_once __DIR__ . '/db_connect.php';


    $db = new DB_CONNECT();


    $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");


    if ($result) {
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";
    } else {
        $response["success"] = 2;
        $response["message"] = "Oops! An error occurred.";
    }
} else {
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
}

echo json_encode($response);