1
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" style="display:inline"  method="post">
    <input type="submit" name="del" value="Delete">

    <br>

    <?php

    $con=mysql_connect("localhost","root") OR DIE ("cant connect to server".mysql_error());

    mysql_select_db("bedanshare") OR DIE ("cannot select db".mysql_error());

    $stud=mysql_query("SELECT * FROM studentreg");

    $count=mysql_num_rows($stud);

    echo "<table border='1'>";
    echo "<th><center />#</th>"; 
    echo "<th><center />Student Number</th>";
    echo "<th><center />First Name</th>";
    echo "<th><center />Last Name</th>";
    echo "<th><center />E-mail</th>";
    echo "<th><center />Username</th>";
    echo "<th><center />Password</th>"; 

    while($row=mysql_fetch_array($stud))
    {
    echo "<tr>";?>
    <td><input name='checkbox[]' type='checkbox' id='checkbox[]' value="<?php echo $row['ID']; ?>">
    <?php
    echo "<td><center />".$row['ID']."</td>";
    echo "<td><center />".$row['Fname']."</td>";
    echo "<td><center />".$row['Lname']."</td>";
    echo "<td><center />".$row['EmailAddress']."</td>";
    echo "<td><center />".$row['Username']."</td>";
    echo "<td><center />".$row['Password']."</td>";  
    echo "</tr>";
    }
    echo "</table>";

    if($del)
    {
    for($i=0;$i<$count;$i++)
    {
    $id=$checkbox[$i];
    $dels=mysql_query("DELETE FROM studentreg WHERE ID='$id'");
    }

    }

    ?>

    </form>

how come the code doesnt delete rows? :( help please. no data seem to be affected. ID is my primary key, however when I click delete, nothing happens. i dont think my query is wrong. btw im using checkboxes to delete multiple rows.

1
  • 1
    1. mysql_ functions are being deprecated. Use mysqli_ or PDO functions instead. 2. Your script is vulnerable to SQL injection. Commented Oct 15, 2012 at 17:36

2 Answers 2

2

There's a condition:

if($del)

Only if it's true it will enter the loop and will start deleting rows. The problem is that the variable $del is not defined in your code.

Edit: Change your condition to:

if(array_key_exists('del',$_POST))

and change the next line as well, from:

$id=$checkbox[$i];

to:

$id=$_POST['checkbox'][$i];
Sign up to request clarification or add additional context in comments.

Comments

0

In your code there is two bugs. 1. $del is undefined variable. 2. $checkbox[] is undefined variable.

You have to modify above code as shown below:

if(isset($_POST['delete']))   // Not if($del)
$id = $_POST['checkbox'][$i]; // Not $id=$checkbox[$i];

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.