【问题标题】:Update in SQL through PHP change's only the first row value在 SQL 中通过 PHP 更改仅更新第一行值
【发布时间】:2023-03-05 23:23:02
【问题描述】:

我的脚本有问题。所以我有一个表,我在 php 的帮助下将 db 中的值提取到 html 表中。然后我有 javascript 代码在从数据库中获取的表格的每一行下方显示 html 表单。 我的表单中有一个隐藏的输入元素,用于获取提交的表单的唯一 ID 然后我使用 ajax 脚本将请求提交到 Php 文件中。

问题是,如果我从我的第一行提交表格,则该特定行的值会发生变化(这对我有用)。但是,如果我从其他行提交表单,则该特定行的值保持不变。 我试图从过去的 3 个小时中弄清楚这一点。需要帮助!谢谢

我有一个 php 脚本:

<?php
foreach($results as $data){
    echo '<tbody>
            <tr class="dropDown">
                <td>1</td>
                <td>'.$data['Title'].'</td>
                <td>'.$data['criticality'].'</td>
                <td>'.$data['Priority'].'</td>
                <td>'.$data['Description'].'</td>
                <td>'.$data['Date_Submitted'].'</td>
                <td>'.$data['NO'].'</td>
                <td></td>
            </tr>
        </tbody>';
}
?>

而我的 Html 表单是在同一个 for 循环中

<form action="/action_page.php">
    <input type="text" name="rowID" value='.$data['NO'].'>
    <fieldset>
        <label>XYZ Questions </label><br>
        <label class="radio-inline">
            <input type="radio" name="optradio">
            <label>YES</label>
        </label>
        <label class="radio-inline left">
            <input type="radio" name="optradio">
            <label>NO</label>
        </label>
    </fieldset>
    <fieldset>
        <label>XYZ Questions </label><br>
        <label class="radio-inline">
            <input type="radio" name="optradio1">
            <label>YES</label>
        </label>
        <label class="radio-inline left">
            <input type="radio" name="optradio1">
            <label>NO</label>
        </label>
    </fieldset>
    <div class="checkbox">
        <label><input type="checkbox"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

我的 ajax 脚本:

var launchAjax = function () { // event handler for button click
    $.get("php/inbetween.php/", {
        id: $("[name=rowID]").val(),      // getting the unique if of each form
        question: $("[name=optradio]:checked").val(),
        question1: $("[name=optradio1]:checked").val(),
    });
}
$("#no").click(launchAjax);

还有我的 php 和 sql:

<?php
    include 'common.php';

    $id = filter_input(INPUT_GET, "id", FILTER_SANITIZE_NUMBER_INT);
    $question = filter_input(INPUT_GET, "question", FILTER_SANITIZE_STRING);
    $question1 = filter_input(INPUT_GET, "question1", FILTER_SANITIZE_STRING);

    function getMark($answer, $mark = 1) {
        $result = 0;
        if($answer == 'YES'){
            $result = $mark;
        }
        return $result;
    }
    $p = 0;
    $p += getMark($question, 1); // provide the answer and the mark
    $p += getMark($question1, .5);
    $c = 0;
    $c += getMark($question, 0.5); // provide the answer and the mark
    $c += getMark($question1, 1);

    $command1 = "UPDATE rating SET criticality = '$c' , Priority = '$p'
    WHERE no = '$id'";

    // prepare and executing
    $stmt1 = $dbh->prepare($command1);
    $result1 = $stmt1->execute();
?>

【问题讨论】:

  • // getting the unique if of each form 如果你有多个 name="rowID" 的元素,那么 jquery 不会这样做。
  • 您的代码有问题:'.$data['NO'].' 绝对不是有效的 HTML
  • 另一个问题:Little Bobbyyour script is at risk for SQL Injection Attacks。你已经有一个准备好的语句,但是你用错了。您仍在将数据放入 SQL 字符串中。更多信息请见MySQLi Prepared Statements
  • 非常感谢您回复我。拜托,谁能建议我如何解决这个问题。
  • 你有&lt;input type="text" name="rowID" value='.$data['NO'].'&gt;,但是你的SQL语句在哪里得到这个$data['NO']值?我建议做类似&lt;input type="text" name="rowID" value="&lt;?php echo $data['NO'] ?&gt;"&gt; 的事情,不应该将NO 小写为no?因为你有你的UPDATE sql WHERE no = '$id'

标签: php jquery html sql ajax


【解决方案1】:

您可以将此输入包含到您的表单中

<input type="hidden" name="rowId" value='.$data['NO']'>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2014-09-09
    • 1970-01-01
    • 2021-08-20
    • 2021-04-21
    • 1970-01-01
    相关资源
    最近更新 更多