2014-08-29 206 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>