1

I am trying very hard for the last 2 hours but am not able to understand. Why am I getting undefined variable in DELETE. Please please look in to my code and help me out.

My main intention is to delete multiple rows at once using checkboxes.

When I am trying to run the below codes it gives me following errors:

Undefined variable: delete in C:\xampp\htdocs\xampp\Test\HRMS\try\search1.php on line 61

But I already have used it in following way:

<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>

Thanks in advance.

my php codes :

  <?php
  $host="localhost"; // Host name
  $username="a"; // Mysql username
  $password="a"; // Mysql password
  $db_name="b"; // Database name
  $tbl_name="emp_info"; // Table name

  // Connect to server and select databse.
  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");

  $sql="SELECT * FROM $tbl_name";
  $result=mysql_query($sql);

  $count=mysql_num_rows($result);

  ?>
  <table width="400" border="0" cellspacing="1" cellpadding="0">
  <tr>
  <td><form name="form1" method="post" action="">
  <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
  <td bgcolor="#FFFFFF">&nbsp;</td>
  <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
  </tr>
  <tr>
  <td align="center" bgcolor="#FFFFFF">#</td>
  <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>Address</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>Source</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>salary</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>Zip</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>Mobile</strong></td>
  <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
  </tr>
  <?php
  while($rows=mysql_fetch_array($result)){
  ?>
  <tr>
  <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['eid']; ?>"></td>
  <td bgcolor="#FFFFFF"><? echo $rows['eid']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['ename']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['address']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['source']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['salary']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['zip']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['mobile']; ?></td>
  <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
  </tr>
  <?php
  }
  ?>
  <tr>
  <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
  </tr>
  <?php
  // Check if delete button active, start this
  if($delete){
  for($i=0;$i<$count;$i++){
  $del_id = $checkbox[$i];
  $sql = "DELETE FROM $tbl_name WHERE eid='$del_id'";
  $result = mysql_query($sql);
  }

  // if successful redirect to search1.php
  if($result){
  echo "<meta http-equiv=\"refresh\" content=\"0;URL=search1.php\">";
  }
  }
  mysql_close();
  ?>
  </table>
  </form>
  </td>
  </tr>
  </table>
0

2 Answers 2

3

Register globals is off (and this is good) so the line

if($delete){

should be

if (isset($_POST['delete'])) { ...

Moreover you need to restructure your code as it's absolutely unnecessery to make a select query to database when you have a post request to delete records.

Here is a complete code you need. Just replace yours with this one.

<?php

error_reporting(E_ALL);

$host = "localhost"; // Host name
$username = "a"; // Mysql username
$password = "a"; // Mysql password
$db_name = "b"; // Database name
$tbl_name = "emp_info"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Delete records
if (isset($_POST['delete']))
{
    // Concatenate ids in a comma-separated string
    $ids = implode(',', $_POST['checkbox']);

    if (!empty($ids))
    {
        $sql = "DELETE FROM $tbl_name WHERE eid IN ($ids)";
        $result = mysql_query($sql) or die(mysql_error());
    }

    // Redirect back
    header('Location: search1.php');
    die();
}

// Select records
$sql = "SELECT * FROM $tbl_name";
$result = mysql_query($sql);

$count = mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
        <td>
            <form name="form1" method="post" action="">
                <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
                    <tr>
                        <td bgcolor="#FFFFFF">&nbsp;</td>
                        <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong></td>
                    </tr>
                    <tr>
                        <td align="center" bgcolor="#FFFFFF">#</td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
                        <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
                    </tr>
                    <?php while ($rows = mysql_fetch_array($result)) { ?>
                    <tr>
                        <td align="center" bgcolor="#FFFFFF">
                            <input name="checkbox[]" type="checkbox" id="checkbox[]"
                                   value="<?php echo $rows['eid']; ?>">
                        </td>
                        <td bgcolor="#FFFFFF"><?php echo $rows['eid']; ?></td>
                        <td bgcolor="#FFFFFF"><?php echo $rows['ename']; ?></td>
                    </tr>
                    <?php } ?>
                    <tr>
                        <td colspan="5" align="center" bgcolor="#FFFFFF">
                            <input name="delete" type="submit" id="delete" value="Delete">
                        </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

10 Comments

First of all thanks for the attention but it is still showing the same error... Notice: Undefined variable: delete in C:\xampp\htdocs\xampp\Test\HRMS\try\search1.php on line 61 :(
I just copied and pasted your code /*[if ($_POST['delete']) { ]*/ in place of my code but it is showing the same error...
You suppose to make sure that $_POST['delete'] is defined using isset or array_key_exists in case $_POST['delete'] = NULL
Absolutely there should be isset.
@Nazariy : I applied isset; now it is not showing any error but it is not deleting even when I click on delete button :(
|
0

Before looping results from MySQL result variable, you should ALWAYS have to make sure that it contain any values:

if($total > 0)
{
    while ($rows = mysql_fetch_array($result))
    {
        //your code goes here
    }
}

Another issue you have here, is same as in your previous question, variable $delete is not defined, based on your code, it is not clear where it is coming from.

So, if $delete is actually a $_POST variable, your if statement should look like this:

if(isset($_POST['delete'])){}

Another issue, $checkbox it is not defined to. I would recommend you to read more about filter_input method as it can be very handy on sanitizing user submitted data.

Also you are using incorrect html tags instead of:

<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>

use correct TD tag which aligns text in center and makes it bold, also bgcolor should be defined in CSS file.

<th bgcolor="#FFFFFF">Id</th>

Your final code should look like this:

<?php
$host = "localhost"; // Host name
$username = "a"; // Mysql username
$password = "a"; // Mysql password
$db_name = "b"; // Database name
$tbl_name = "emp_info"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Make sure that delete action was called using
//$delete = isset($_POST['delete']);
//OR
//$delete = array_key_exists('delete', $_POST);
//OR
$delete = filter_input(INPUT_POST, 'delete');

$selected = filter_input(INPUT_POST, 'checkbox', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY);

if ($delete && !empty($selected))
{

    foreach ($selected as $id)
    {
        $sql = "DELETE FROM $tbl_name WHERE eid='$id'";

        $result = mysql_query($sql);
    }

    // if successful redirect to search1.php
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=search1.php\">";
}

$sql = "SELECT * FROM $tbl_name";

$result = mysql_query($sql);

$count = mysql_num_rows($result);

?>


<form name="form1" method="post" action="">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
        <tr>
            <th bgcolor="#FFFFFF">&nbsp;</th>
            <th colspan="9" bgcolor="#FFFFFF">Delete multiple rows in mysql</th>
        </tr>
        <tr>
            <th bgcolor="#FFFFFF">#</th>
            <th bgcolor="#FFFFFF">Id</th>
            <th bgcolor="#FFFFFF">Name</th>
            <th bgcolor="#FFFFFF">Password</th>
            <th bgcolor="#FFFFFF">Address</th>
            <th bgcolor="#FFFFFF">Source</th>
            <th bgcolor="#FFFFFF">salary</th>
            <th bgcolor="#FFFFFF">Zip</th>
            <th bgcolor="#FFFFFF">Mobile</th>
            <th bgcolor="#FFFFFF">Email</th>
        </tr>
        <?php
        if ($count)
        {
            while ($rows = mysql_fetch_array($result))
            {
                ?>
                <tr>
                    <td align="center" bgcolor="#FFFFFF">
                        <input name="checkbox[]" type="checkbox" id="checkbox" value="<?php echo $rows['eid']; ?>">
                    </td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['eid']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['ename']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['password']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['address']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['source']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['salary']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['zip']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['mobile']; ?></td>
                    <td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
                </tr>
                <?php
            }
        }
        mysql_close();
        ?>
        <tr>
            <td colspan="10" align="center" bgcolor="#FFFFFF">
                <input name="delete" type="submit" id="delete" value="Delete">
            </td>
        </tr>
    </table>
</form>

Please don't just copy paste it, read it and try to understand it.

7 Comments

when I apply isset it does not show any error but it is not deleting when I click on delete button :(
It is not showing any error but its still not deleting. My column field name is eid which is same; m keeping in the query. It should match to the table even though I do not know where is it going wrong. If u do little more with these codes then this must be working as it did yesterday. Please... check the codes... Thanks in advance...
You had so many issues that I forgot to place delete functionality in beginning of your code. Give it a try again and try to understand why it's written this way.
@ Nazariy :Thanks a lot for your precious time... I checked your codes... Its becoming quite difficult for me to understand why is it not deleting from the table when m using correct column name :( cheers...
try to inspect your html output and make sure that checkbox value is set in HTML SOURCE, also if you are using MySQL InnoDB tables and it has some foreign keys linked from another table, it might prevent from removing records.
|

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.