PHP MYSQLI无法从数据库中删除一行数据

时间:2022-09-26 11:01:27

i want to delete a row of data for example userid number 4 when i click on the delete button but nothing happen.It just pop out "Are you sure you want to delete '4undefine" Can someone help?

我想删除一行数据,例如当我点击删除按钮但没有任何事情发生时,例如用户ID号为4。它只是弹出“你确定要删除'4undefine”有人可以帮忙吗?

index.php

<?php
  $connection = mysqli_connect("localhost","root","","userdatabase");
  mysqli_select_db($connection, "userdatabase");
  $sql_query="SELECT * FROM user";
  $result_set=mysqli_query($connection,$sql_query);
  if(isset($_GET['delete_id']))
  {
    $sql_query="DELETE FROM user WHERE userid=".$_GET['delete_id'];
    mysqli_query($sql_query);
    header("Location: $_SERVER[PHP_SELF]");
  }
?>

This is the confirmation

这是确认

<script type="text/javascript">
function delete_id(userid,username,password,email,workplace,role)
{
  if(confirm("Are you sure you want to delete '" +userid+username))
  {
    window.location.href='adminhome.php?delete_id='+userid;
  }
}
</script>




<?php
 if(mysqli_num_rows($result_set)>0)
 {
    while($row=mysqli_fetch_row($result_set))
    {
        $userid = $row[0];
        $username = $row[1];
        $password = $row[2];
        $email = $row[3];
        $workplace = $row[4];
        $role = $row[5];
        ?>
        <tr>
        <td><?php echo $userid; ?></td>
        <td><?php echo $username; ?></td>
        <td><?php echo $password; ?></td>
        <td><?php echo $email; ?></td>
        <td><?php echo $workplace; ?></td>
        <td><?php echo $role; ?></td>

        <td align="center"><a href="javascript:delete_id(<?php echo $row[0]; ?>)"><img src="RemoveUser.png" alt="Delete" /></a></td>
        </tr>
        <?php
    }
  }

1 个解决方案

#1


0  

So your first problem is that you've defined your function like this:

所以你的第一个问题就是你已经定义了这样的函数:

function delete_id(userid,username,password,email,workplace,role)

But are calling it like this:

但是这样称呼它:

delete_id(<?php echo $row[0]; ?>)

Is it any wonder that username is undefined? Then your syntax is incorrect on your call to mysqli_query(), as pointed out in the comments above. It takes two parameters in the procedural style you're using.

用户名未定义是否奇怪?然后,在您调用mysqli_query()时,语法不正确,如上面的注释所述。它在您使用的过程样式中需要两个参数。

Next thing you need to worry about is what would happen if someone went to http://your.site/adminhome.php?delete_id=4%20or%201=1. I suggest you learn about preventing SQL injections ASAP.

接下来你需要担心的是,如果有人去http://your.site/adminhome.php?delete_id=4%20or%201=1会发生什么。我建议你尽快学习防止SQL注入。

#1


0  

So your first problem is that you've defined your function like this:

所以你的第一个问题就是你已经定义了这样的函数:

function delete_id(userid,username,password,email,workplace,role)

But are calling it like this:

但是这样称呼它:

delete_id(<?php echo $row[0]; ?>)

Is it any wonder that username is undefined? Then your syntax is incorrect on your call to mysqli_query(), as pointed out in the comments above. It takes two parameters in the procedural style you're using.

用户名未定义是否奇怪?然后,在您调用mysqli_query()时,语法不正确,如上面的注释所述。它在您使用的过程样式中需要两个参数。

Next thing you need to worry about is what would happen if someone went to http://your.site/adminhome.php?delete_id=4%20or%201=1. I suggest you learn about preventing SQL injections ASAP.

接下来你需要担心的是,如果有人去http://your.site/adminhome.php?delete_id=4%20or%201=1会发生什么。我建议你尽快学习防止SQL注入。