Reputation: 49
Trying to populate a mysql database, but if i send the variables with postman or android studio i get this
<!DOCTYPE html>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>404 Not Found</body>
</html>
this is my php code, i leftout the db username and such for security but i assure you they are correct
<?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);
}
?>
Upvotes: 0
Views: 66
Reputation: 94672
I bet you are not looking at your PHP Error log are you!!!
You are not looking for a connection error after running a query you are just looking for an error
$result = $conn->query($sql) or die (mysqli_connect_error());
should be
$result = $conn->query($sql) or die ($conn->error);
When using the mysqli_
API it is a good idea Add
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
to the top of your script While Testing only.
This will force any mysqli_
errors to generate an Exception that you cannot miss or ignore.
But like I said in a comment. If your JAVA code gets a 404 error when trying to call this script, then you are not calling this script correctly. You dont show your java code so we cannot help in the debugging of that.
Upvotes: 3