2016-08-24 59 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