gpt4 book ai didi

php - 网页表单不发送输入到mysql数据库

转载 作者:行者123 更新时间:2023-11-29 10:51:40 26 4
gpt4 key购买 nike

我有一个网页需要将数据发送回 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>

最佳答案

我不知道您是否更改了数据库中表行的名称,但假设它们与代码中的相同,我已经更正了您的代码,以便从您尝试插入的字段中提取在表单中输入正确的内容。

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>

关于php - 网页表单不发送输入到mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43622931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com