2014-08-29 153 views
0

您好我一直在关注这个教程 http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ 我试图创建一个行(部分6),但我总是得到“必填字段丢失”的消息错误在PHP和数据库连接

我甚至尝试下载代码,但没有运气

我已经在phpmyadmin按照指示做了正确的数据库,我做错了什么?

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

不要使用mysql_ *函数检查PHP手册替代品。 – 2014-08-29 19:45:47

+0

如果该教程告诉你写这样的查询,那么你应该把这个教程看作是有毒的浪费:你现在很容易受到[sql注入攻击](http://bobby-tables.com)的攻击, t显示你的任何android端代码和它发送的内容,我们无法真正帮助你。 – 2014-08-29 19:45:58

+0

您是否使用所需参数进行POST请求? – Populus 2014-08-29 19:46:01

回答

0

您必须通过POST提交名称,价格和描述。

做一个HTML文件与此代码,并填写数据,它应该工作:

<form action="create_product.php" method="post" name="submit_data"> 
Name: <input type="text" name="name" value="" /><br /> 
Price: <input type="text" name="price" value="" /><br /> 
Description: <input type="text" name="description" value="" /> 
</form>