【问题标题】:How do I update a row of data with a button click如何通过单击按钮更新一行数据
【发布时间】:2023-04-04 14:15:01
【问题描述】:

首先我是 PHP 新手。

我被要求更改一些代码并添加选项以查看字段的状态并标记已完成的行。因此,我在我的 MySQL 数据库中添加了一个状态字段,其标准值为 0。然后在单击按钮时,我希望将其更改为值 1。在此基础上,我可以使用按钮投影一个十字字形图标更新此内容,或勾选复选标记以使状态为“完成”。

现在我遇到了更新部分的问题。我似乎无法通过单击按钮并仅更新该行来使其工作。如果有用的话,字段“prdh_num”是我的唯一值。

这是我使用的代码:

    function uren_error(){
    global $mysql;
    $sql = "
                SELECT * 
                FROM uren_error, medw
                WHERE uren_error.uren_datum between adddate(now(),-14) and now() AND medw.medw_num = uren_error.medw_num
                ORDER BY uren_error.prdh_num";
    $query = $mysql->query($sql);

    echo "<h1>Niet in MKG verwerkte uren van de afgelopen 14 dagen</h1>";
    echo "<div class='row' id='urenTabel'>";
    echo "<div class='col-xs-0 col-md-0'></div>";
    echo "<div class='col-xs-12 col-md-12'>";
    echo "<table class='table'>";
    echo "<thead>";
    echo "<th>Verwerkingsdatum</th>";
    echo "<th>Medewerker</th>";
    echo "<th>Productieorder</th>";
    echo "<th>Productieorder regel</th>";
    echo "<th>Bewerking</th>";
    echo "<th>Duur</th>";
    echo "<th>Status</th>";
    echo "<th>Actie</th>";
    echo "</thead>";
    while($row = mysqli_fetch_array($query)){

        $hours = floor($row['uren_tijd_man_std'] / 3600);
        $mins = floor($row['uren_tijd_man_std'] / 60 % 60);
        echo "<tr>";
        echo "<td>" .$row['uren_datum']. "</td>";
        echo "<td>" .$row['medw_roepnaam'] . " " . $row['medw_tussenvoegsel'] . " " . $row['medw_naam'] . "</td>";
        echo "<td>" .$row['prdh_num']. "</td>";
        echo "<td>" .$row['prdr_num']. "</td>";
        echo "<td>" .$row['bewerk_num']. "</td>";
        echo "<td>" .$hours.":".$mins. "</td>";
        if($row['uren_status'] == "0"){
            echo "<td><div class='glyphicon glyphicon-remove' style='color:red'></div></td>";
            echo "<td><form action'' method='POST'><input type='submit' name=".$row['prdh_num']."></input></form></td>";
        } else if($row['uren_status'] == "1"){
            echo "<td><div class='glyphicon glyphicon-ok' style='color:green'></div></td>";
        }
        echo "</tr>";
    }
    echo "</table>";
}

到目前为止我的更新代码:

$productieorder = $row['prdh_num'];
$updateStatus = "UPDATE uren_error SET uren_status = 1 WHERE prdh_num=".$productieorder."";
$query2 = $mysql->query($updateStatus);

【问题讨论】:

  • 点击submit按钮,你在哪里更新&lt;form action'' method='POST'&gt;..&lt;/form&gt;你的action是空的,你也错过了action附近的=,即:action='',你需要使用@ 987654329@获取价值。
  • 我已经对其进行了一些更改,以便您可以看到我到目前为止的更新代码。它确实更新了数据库中的每一个条目,但在按钮单击时并没有完全更新,只有那一行。但似乎我需要先创建一个新文件并将其添加为我的操作?

标签: php mysql


【解决方案1】:

更简单的方法是你可以使用&lt;a&gt;标签来更新你的记录,而不是&lt;form&gt;,只是附加你需要传递的参数。即:

echo "<td><a href='currentphppagename.php?prdh_num=".$row['prdh_num']."'></a></td>";

要获得prdh_num,请在同一页面上使用$_GET,即:

 //check if have some value or not
    if (isset($_GET['prdh_num'])) {
    //getting value passed in url
    $productieorder =  $_GET['prdh_num'];
    $updateStatus = "UPDATE uren_error SET uren_status = 1 WHERE prdh_num=".$productieorder."";
    $query2 = $mysql->query($updateStatus);
    }

而使用form 的其他方式,您可以拥有一个隐藏字段并将row 值分配给该字段:

echo "<td><form action='' method='POST'><input type='hidden' value=".$row['prdh_num']." name='prdh_num'/><input type='submit' name='submit'></input></form></td>";

现在点击submit 在同一页面上查看以下内容:

//check if have some value or not
    if (isset($_POST['submit'])) {
    //getting value passed in url
    $productieorder =  $_POST['prdh_num'];
    $updateStatus = "UPDATE uren_error SET uren_status = 1 WHERE prdh_num=".$productieorder."";
    $query2 = $mysql->query($updateStatus);
    }

【讨论】:

  • 表单的第二种方式就像一个魅力!我在 PHP 代码下方添加了一个重定向,以便它自动将用户发送回之前的页面。谢谢!
【解决方案2】:

您可以使用 javascript 来做到这一点。您应该在 tr 标签上放置“ondblclick”事件。

<tr ondblclick="Update(rowno)" />

并且在javascript标签中,必须有类似的函数

    <script type="text/javascript">
        function Uptdate(row) {
            window.location.href ="update.php?row="+row;
        }
    </script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多