0
<?php
include("connection.php");
echo "Do you really want to DELETE this field? " ;
?> 
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
$res = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($res))
{
if(isset($_REQUEST['yes']))
{
    $id=$row['id'];
    echo $id;
    mysql_query("DELETE from members WHERE id= '$id' "); 
    header("location: view.php");
}
}
?>
<input type="submit" name="no" value="NO" />
<?php
if(isset($_REQUEST['no']))
{
    header("location: view.php");
}
?>
</form> 
</body>

This is my code for delet random row from list view when click on delete button but i m not getting the result so please help me out of these.

  • 2
    Why are people *still* using `mysql` functions? For the love of god, please switch to `mysqli` or `pdo`. – Christian Apr 23 '14 at 08:17

4 Answers4

0

Change this line:

mysql_query("DELETE from members WHERE id= '$id' "); 

to that:

mysql_query("DELETE FROM members WHERE id= '".$id."' ");
cyborg86pl
  • 2,597
  • 2
  • 26
  • 43
0

Pass db connection as second parameter to your mysql_query() to delete rows from members table:

mysql_query("DELETE from members WHERE id= '$id'" , $dbCon); 

Here DB connection I mean something this that you have defined in your connection.php file:

$db_host="localhost";
$db_name="test";
$username="root";
$password="";
$dbCon=mysql_connect($db_host,$username,$password);

I hope this solve your problem!

Deepak Goswami
  • 2,030
  • 1
  • 16
  • 22
0

Your query SELECT * FROM members without ordering will select all the lines in a unpredictable order.

The ifs in your while loop will be either always true or always false:

  • if the user click on the YES button, all the rows of the table will be deleted as you don't have exit; after the header("location: view.php"); (see this post)

  • if the user click the NO button, then every iteration of the while loop won't do nothing.

So what you should do:

  • Have an hidden parameter in your form containing the id of the field you want to delete.
  • Test when the user submit the form that the hidden field is present and sanitized.
  • Do not select all the rows of the table
  • Delete the rows secificaly by its primary key.
  • Call exit; after header("location: view.php");
  • When do an if inside a loop, move it outside the loop if possible if the values do not change.
Community
  • 1
  • 1
fluminis
  • 3,575
  • 4
  • 34
  • 47
0
<?php
include("connection.php");

echo "Do you really want to DELETE this field? " ;
?> 
<form method="post">
<input type="submit" name="yes" value="YES" />
<?php
if(isset($_REQUEST['yes']))
{
    if(isset($_GET['delet']))
    {
        $id = $_GET['delet'];
        $res = mysql_query("SELECT * FROM members WHERE id = '$id' ");
        $row = mysql_fetch_array($res);
        $did = $row['id'];
        $dname = $row['name'];
        $dphn = $row['phn'];
        /*echo $did;
        echo $dname;
        echo $dphn;*/

        $ins = "INSERT INTO del (mid,name,phn) values ('$did','$dname','$dphn')";
        mysql_query($ins);

        mysql_query("DELETE FROM members WHERE id = '$id' ");
        header('location: view.php');

    }
}

?>  

<input type="submit" name="no" value="NO" />

<?php
if(isset($_REQUEST['no']))
{
    header("location: view.php");
    exit;
}
?>
</form>