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

时间:2015-11-27 18:07:20

标签: javascript php mysqli

我想删除一行数据,例如当我点击删除按钮但没有任何事情发生时,例如用户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]");
  }
?>

这是确认

<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 个答案:

答案 0 :(得分:0)

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

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

但是这样称呼它:

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

username未定义是否奇怪?然后,在您对mysqli_query()的调用中,您的语法不正确,如上面的评论所述。 It takes two parameters处于你正在使用的程序风格中。

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