dsjswclzh40259075 2016-08-08 09:46
浏览 112
已采纳

使用复选框和PHP更新MySQL数据库

I am trying to update a MySQL database with checkboxes and PHP. I have read a lot of code examples online and read many questions on here but I seem to be stuck at the last hurdle.

My code first queries MySQL to bring back a list of users and then a checkbox (which is either 0 or 1 in MySQL) next to each one, indicating whether or not the user is completed.

What I am wanting to do is when the checkbox is checked, for that to update the MySQL database and update the column with 1, or if it is unchecked, for it to change the column to 2.

Here is my code so far:

HTML Snippet (Checkboxes):

        while($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>" . $row['Firstname'] . "</td>";
            echo "<td>" . $row['Surname'] . "</td>";
            echo "<td>" . $row['Department'] . "</td>";
            echo "<td>" . $row['RequestedBy'] . "</td>";
            echo "<td>" . $row['StartDate'] . "</td>";
            echo "<td class='tbl-chk'> <input type='checkbox' name='WSCompleted' value='"; echo $row['ID'] . "'" ; if ($row['WSCompleted']=='1') { echo "checked='checked'";} echo "/></td>";

Here is my jQuery that successfully retrieves the values and IDs and then posts them:

        $(document).ready(function(){
        $('input[name=WSCompleted]').click(function(){
            var wsCompleted = $(this).is(':checked') ? 1 : 0;
            var wsCompletedID = $(this).attr('value');

            $.ajax({
            type: "POST",
            url: "/usr-handler.php",
            data: {id: wsCompletedID, wsCompleted: wsCompleted},
            success: function(){
                $('div.success').fadeIn();
            }
        });
        return true;

        }); 
    });

And finally here is a snippet of my PHP:

$wsCompleted = $_POST['wsCompleted'];   
$id = $_POST['wsCompletedID'];

$query = "UPDATE newusers SET WSCompleted = '$wsCompleted' WHERE id = '$id'";

mysqli_query($con, $query) or die(mysqli_error());


mysqli_close($con);

I am setting the value of the checkbox to what is actually the row ID in MySQL, then I can use the checkbox value to select the correct row in MySQL, and update it.

The problem I am having is that currently, how the code is, I get the following response from FireBug:

Unidentified index: WSCompletedID

After looking online it was suggested to change:

$id = $_POST['wsCompletedID'];

To:

$id = (ISSET($_POST['wsCompletedID']));

But doing so clears the error message, but then doesn't actually update the value on MySQL.

If I manually set the $id to something, the update works, but obviously that isn't right as it would only ever update the ID I have chosen it to.

I am completely stumped as to what is causing the problem. I have tried finding out online but cannot get anywhere with it.

Any help is greatly appreciated.

Thank you

  • 写回答

1条回答 默认 最新

  • duan4523 2016-08-08 09:52
    关注

    You are loading data here in your javascript AJAX code

    data: {id: wsCompletedID, wsCompleted: wsCompleted},
    

    This will create the following $_POST array

    id => whatever was in wsCompletedID
    wsCompleted => whatever was in wsCompleted
    

    So your PHP code should be looking for $_POST['id'] and $_POST['wsCompleted'] as these are the names you have given in your data:...

    $wsCompleted = $_POST['wsCompleted'];   
    $id = $_POST['id'];
    
    $query = "UPDATE newusers SET WSCompleted = '$wsCompleted' WHERE id = '$id'";
    
    mysqli_query($con, $query) or die(mysqli_error());
    mysqli_close($con);
    

    HOWEVER: Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared statement and parameterized statements

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘