从下拉菜单更新 mysql 中的行


Update row in mysql from dropdown menu

我有从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();
根据您的

代码,您将再次发送数据库值以确认.php而不是用户选择的值。为此,请为喜欢分配名称..而不是锚点单击使用表单提交。.它会起作用。

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

根据我们之前的评论:

将按钮设为<input type="button" onclick="saveChange('.$id.')"/>

然后将函数"url"设置为users/confirm.php

并将"数据"设置为 id。

    function saveChange(id){
        $.ajax({
                type: "POST",
                url: "users/confirm.php",
                data: 'id='+id
        });
    }


我建议你使用这样的东西:

$.ajax({
        type: "POST",
        url: "save.php",
        data: data,
        success: function(msg){}
        });

对于onchange=""事件,在save.php您只需使用更新方法即可。

顺便一提

$query = "SELECT * FROM reservation WHERE id=$id" or die(mysqli_error($con)); $res = mysqli_query($con, $query);

就像:

$query = "SELECT * FROM reservation WHERE id=$id"; $res = mysqli_query($con, $query) or die(mysqli_error($con));