我有一个网页,需要将数据发送回MySQL数据库,但前两个表格框数据不会发送到数据库。
我的php mysql知识非常有限,无法自己解决问题。
//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>
答案 0 :(得分:1)
我不知道您是否更改了数据库中表行的名称,但假设它们与代码中的相同,我更正了您的代码,以便您尝试插入的字段是从表格中的正确输入中提取。
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'])) {
$outputname = mysqli_real_escape_string($conn,$_POST['outputname']);
$outputip = mysqli_real_escape_string($conn,$_POST['outputip']);
$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 ('$outputname', '$outputip','$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="outputname" 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>