2016-08-24 84 views
0

試圖填充一個MySQL數據庫,但如果我有郵差或Android工作室發送變量我得到這個PHP代碼沒有乳寧

<!DOCTYPE html> 
<html> 
    <head> 
     <title>404 Not Found</title> 
    </head> 
    <body>404 Not Found</body> 
</html> 

,這是我的PHP代碼,我leftout安全數據庫的用戶名和這樣的但我向你保證,他們是正確的

<?php 

/* 
* Following code will create a new product row 
* All product details are read from HTTP Post Request 
*ordernum,pilotname,pilotcash,date,planemodel,hoobsstart,hoobsend,watchtime,hoobstime ,gas ,liter ,repairname ,repaircost ,travelexpense,othername1,othercost1 
*/ 

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

// check for required fields 
if (isset($_POST['pilotname']) && isset($_POST['pilotcash']) && isset($_POST['date']) && isset($_POST['planemodel'])&& isset($_POST['hoobsstart'])&& isset($_POST['hoobsend'])&& isset($_POST['watchtime'])&& isset($_POST['hoobstime'])&& isset($_POST['gas'])&& isset($_POST['liter'])&& isset($_POST['repairname'])&& isset($_POST['repaircost'])&& isset($_POST['travelexpense'])&& isset($_POST['othername1'])&& isset($_POST['othercost1'])) { 

    $pilotname = $_POST['pilotname']; 
    $pilotcash = $_POST['pilotcash']; 
    $date = $_POST['date']; 
    $planemodel = $_POST['planemodel']; 
    $hoobsstart = $_POST['hoobsstart']; 
    $hoobsend = $_POST['hoobsend']; 
    $watchtime = $_POST['watchtime']; 
    $hoobstime = $_POST['hoobstime']; 
    $gas = $_POST['gas']; 
    $liter = $_POST['liter']; 
    $repairname = $_POST['repairname']; 
    $repaircost = $_POST['repaircost']; 
    $travelexpense = $_POST['travelexpense']; 
    $othername1 = $_POST['othername1']; 
    $othercost1 = $_POST['othercost1']; 


    define('DB_USER', "`enter code here`"); // db user 
define('DB_PASSWORD', ""); // db password (mention your db password here) 
define('DB_DATABASE', ""); // database name 
define('DB_SERVER', ""); // db server 
// array for JSON response 



$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE); 


$sql = "INSERT INTO orden(pilotname,pilotcash,date,planemodel,hoobsstart,hoobsend,watchtime,hoobstime,gas,liter,repairname,repaircost,travelexpense,othername1,othercost1) VALUES('$pilotname','$pilotcash','$date','$planemodel','$hoobsstart','$hoobsend','$watchtime','$hoobstime','$gas','$liter','$repairname','$repaircost','$travelexpense','$othername1','$othercost1')"; 

    // mysql inserting a new row 
    $result = $conn->query($sql) or die (mysqli_connect_error()); 

    // 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

沒有運行?它應該是由於某種原因 –

+0

你發送什麼樣的請求。您的腳本需要POST方法。你提到你的問題。順便說一句,如果你的代碼仍然如此,你的數據庫很容易被sql注入。 –

+0

這是一個錯字,即時通訊使用郵遞員或android studio發送它需要運行的變量,但我沒有迴應,我應該得到$ response [「success」] = 1; $ response [「message」] =「產品成功創建。」;或$ response [「success」] = 0; $ response [「message」] =「必填字段丟失」;或$ response [「success」] = 0; $ response [「message」] =「糟糕!發生錯誤。」;相反,我得到<!DOCTYPE HTML> 404未找到 404未找到 – Aimatos

回答

4

我敢打賭,你不看你的PHP錯誤日誌是你!

您沒有運行你只是在尋找一個錯誤的查詢後,尋找一個連接錯誤

$result = $conn->query($sql) or die (mysqli_connect_error()); 

應該

$result = $conn->query($sql) or die ($conn->error); 

當使用mysqli_ API這是一個好主意Add

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

到腳本的頂部只測試

這將強制任何mysqli_錯誤生成一個您不能錯過或忽略的異常。

但正如我在評論中所說。如果您的JAVA代碼在嘗試調用此腳本時遇到404錯誤,那麼您不會正確調用此腳本。你不顯示你的java代碼,所以我們不能幫助調試。

+0

它做了它,現在它顯示了我的問題是什麼,在我的數據庫中的列的名稱和這裏不同,我有一個小錯字,謝謝一堆,我都是新來的,所以我甚至不知道我的是什麼真正的問題。 – Aimatos