0

i showing my users from database in html table i am make a button for every record in table for delete the record but i do not know how to do that
this is my code:

<table width="200" border="1">

    <?php
    while($row=mysqli_fetch_assoc($result)){
        ?><tr>
            <th scope="col">ID</th>
            <th scope="col">user</th>
            <th scope="row">pass</th>
            <th scope="col">edit</th>

        </tr>


        <td><?php echo $row['id']?></td>
        <td><?php echo $row['user']?></td>
        <td><?php echo $row['pass']?></td>
        <td><a href="delone.php"><button>edit</button></a></td>
        </tr>

        <?php
    }
    ?>


</table>

2 Answers 2

1

I think you are providing 'edit' option for every users. if u want to add delete option, that is : 'id, user, pass' of the user will be deleted. To do that just add this below edit option.

<td><a href="delete.php?id=<?php echo $row['id'] ?>" >Delete </a></td>

Now, using sql query you can delete that information from your db.

if (isset($_GET['id'])) {

      // write query to delete from db
}

Now record will be deleted from your Database. Do it carefully! Happy Coding!!!

Sign up to request clarification or add additional context in comments.

Comments

0

First,You have passed $_row in delete query. $_GET['id'] should be passed in palace of $_row.

Second,mysqli_fetch_assoc($result); This statement has no purpose for deletion

Third, use prepare statements to avoid mysql injections and validate $_GET['id'] value

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$myConnection= mysqli_connect("localhost","root","") or die ("could not connect to mysql"); 

mysqli_select_db($myConnection, "test") or die ("no database");
	
	//query
if (isset($_GET['id'])) {

      // write query to delete from db
	$sql="DELETE FROM `userpass` WHERE id='$_row'";
}
$result=mysqli_query($myConnection,$sql);
mysqli_fetch_assoc($result);
?>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.