gpt4 book ai didi

php - 如何在php中将下拉列表中选定的值保存到mysql

转载 作者:行者123 更新时间:2023-11-29 22:53:32 26 4
gpt4 key购买 nike

<?php 

$status=&$_POST['status'];
$from=&$_POST['date_from'];
$to=&$_POST['date_to'];
$conn=mysqli_connect('localhost','root','','punbus') or die("Database not connected".mysqli_error());

if(isset($_POST['sub'])){
$ins="insert into driver_status(driver_name,status,date_from,date_to)
select Driver_name,'$status','$from','$to' from driver_master";
if(mysqli_query($conn,$ins)){
echo "added";
}
else{
echo "NOT".mysqli_error($conn);
}
}

$sel='select Driver_name from driver_master';
$query=mysqli_query($conn,$sel);

echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";

while($row=mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['Driver_name']."</td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
?>

<td>
<select name="status">
<?php
while($row1=mysqli_fetch_assoc($query1)){
$st=$row1['d_status'];
echo "<option value='$st'>$st</option>";
}
?>
</select>
</td>
<?php
echo "</tr>";

}

echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>

这是我的代码。我想将从 4 个下拉列表中选择的选项保存到 mysql。当我提交表单时,从最后一个框中选择的值将保存在 mysql 表的所有行中。现在,请告诉我我应该做什么?我从数据库表中正确获取下拉框值,那么问题是什么?

最佳答案

这是您的代码。

<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from = $_POST['date_from'];
$to = $_POST['date_to'];
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$s','$from','$to')";
if (mysqli_query($conn, $ins)) {
echo "added";
} else {
echo "NOT" . mysqli_error($conn);
}
}
}

$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);

echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name']
."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>";
}
echo "</select></td></tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>

关于php - 如何在php中将下拉列表中选定的值保存到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28803814/

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