gpt4 book ai didi

php - 从下拉菜单更新 mysql 中的行

转载 作者:行者123 更新时间:2023-11-29 07:47:34 25 4
gpt4 key购买 nike

我有一个从 mysql 填充的表。其中一行是 status,默认情况下为 0,并且有下拉菜单,我可以在其中选择 12。然后有按钮update,它应该更新表中的该行。问题是没有更新。

这是表格

if(isset($_GET['rest_id']) && is_numeric($_GET['rest_id'])){
$rest_id = $_GET['rest_id'];

$query = mysqli_query($con, "SELECT * FROM reservation
WHERE table_res_id = $rest_id
ORDER BY `DateTime` DESC ");

echo "</p>";

// display data in table

echo '<table class="table table-striped table-bordered responsive">';
echo "<thead>";
echo '<tr>
<th>Name</th>
<th>Comment</th>
<th>Status</th>
<th></th>
</tr>
</thead>';
echo '<div class="row">';
echo '<div class="box col-md-12">';
echo '<div class="box-content">';
echo "<tbody>";
// loop through results of database query, displaying them in the table
while ($res = mysqli_fetch_assoc($query))
{

echo '<tr>';
echo '<td>' . $query['name'] . '</td>';
echo '<td>' . $query['comment'] . '</td>';
echo '<td>
<div class="btn-group">
<select>
<ul class="dropdown-menu">
<li><option> Status:'. $query['status'] .'
<span class="caret"></span>
</option></li>
<li><option>1</option></li>
<li><option>2</option></li>
</ul>
</select>
</div>
</td>';
echo '<td>
<a class="btn btn-info" href="users/Confirm.php?id=' . $query['id'] . '">
<i class="glyphicon glyphicon-edit icon-white"></i>
Change status</a></td>';

echo "</tr>";

这是confirm.php

session_start();
include '../misc/db.inc.php';
ob_start();
if (isset($_GET['id']) && is_numeric($_GET['id']))
{

$id = $_GET['id'];
$query = "SELECT * FROM reservation WHERE id=$id" or die(mysqli_error($con));
$res = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($res);
if ($stmt = $con->prepare("UPDATE reservation SET status = ? LIMIT 1"))

{
$stmt->bind_param("i",$res['status');
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: could not prepare SQL statement.";
}
$con->close();

最佳答案

根据您的代码,您将再次发送数据库值到confirm.php,而不是用户选择的值。为此,将名称指定为类似..并且使用表单提交代替 anchor 单击..它将起作用。

另一个解决方案。您可以在 javascript 函数将被调用的位置调用 onchange 事件,并通过 Ajax 更新数据库中的选定值。希望这个逻辑能帮助您

关于php - 从下拉菜单更新 mysql 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27316600/

25 4 0