Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7053073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:27:38+00:00 2026-05-28T03:27:38+00:00

I am trying very hard for the last 2 hours but am not able

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:27:38+00:00Added an answer on May 28, 2026 at 3:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying a very simple demo of SignalR but getting the infamous undefined error
I am trying very hard to make this work but I can't see what
I am having a hard time trying to make the very last UITableViewCell from
I've been trying very very hard to create a simple simple iOS app which
I have been trying very hard to achieve rounded corners with IE6+jquery ui tabs.
I am trying to allow very primitive customization based on being able to pass
I am trying to understand dynamic linq and expression trees. Very basically trying to
I'm having a very hard time trying to do something very simple. Here's the
I've been trying very hard to get gitolite working. I used the package approach
I'm having a very hard time trying to access a custom configuration section in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.