简体   繁体   中英

PHP MYSQLI Unable to delete a row of data from database

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?

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
    }
  }

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM