I have a web page which needs to send data back to a MySQL database but the first two form boxes data is not sent across to the database.
I have very limited php mysql knowledge and cannot figure out the problem on my own.
//create the connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn)
{
//Error handler
die("MySQL Connection Error: ".mysqli_error());
}
//Extracting information from the user to add to the outputs table in the Database
if (isset($_POST['confirmbut'])) {
$sensorname = mysqli_real_escape_string($conn,$_POST['sensorname']);
$sensorip = mysqli_real_escape_string($conn,$_POST['sensorip']);
$state = mysqli_real_escape_string($conn,$_POST['state']);
//Selecting Database and inserting user inputted data to the database
mysqli_query($conn,"SELECT * FROM outputs");
mysqli_query($conn,"INSERT INTO outputs(Sensor_ID, Sensor_IP, State, Pending_Update) VALUES ('$sensorname', '$sensorip','$state','1')");
header("location: insertname.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Switch </title>
</head>
<body>
<h2 class="title">Please fill in your switches Details</h2>
<!-- Posts data inserted here -->
<form method="post" action="insertname.php">
<!-- Form to enter Device Name, Output IP, State -->
<tr>
<td> Relay Name: </td>
<td><input type="text" name="output name" class="textInput" required="required"></td>
</tr>
<tr>
<td> Relay IP:</td>
<td><input type="text" name="outputIP" class="textInput" required="required"></td>
</tr>
<tr>
<td> Relay State: On = 1 Off = 0</td>
<td><input type="text" name="state" class="textInput" required="required"></td>
</tr>
<td><input type="submit" name="confirmbut" value="Enter" class="enterbutton" > </td>
</tr>
</form>
</body>
</html>
mysqli_query($conn,"INSERT[...]? That line inserts data into the database. It is trying to insert data previously defined as$sensornameand$sensorip. This is defined as coming from the form from inputs namedsensornameandsensorip. You don't have inputs in your form namedsensornameandsensorip. And you don't have anywhere in the code that takes the values from the fields you are using namedoutput nameandoutputIP.mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use manual escaping and string interpolation or concatenation to accomplish this because you will create severe SQL injection bugs. Accidentally unescaped data is a serious risk.<form actionis the wrong url, since yourheader('LOCATION:is the same after the post... but really... learn AJAX... and don't do$conn->query('SELECTs for no reason. Yeah, that's the syntax you will probably appreciate if you like less typing. Then you can do stuff like$conn->escape_string($stringHere).