Page 1 of 1

delete data from database using checkbox

Posted: Mon Jan 22, 2007 4:25 am
by shwetarani
hi everyone,
i have to delete the data from the database.but there is one problem, when i checked any checkbox,all data are deleted from database.here is my code.please help me.

Code: Select all

<?php 

$con =  mysqli_connect("localhost", "root", "admin","paras"); 

if (!$con)
  {
  die('Could not connect: ' . mysqli_errno($con));
  }

$sql="select Name, Email, Address, Country, City, Zip, Phone, Mobile, Message,f_id from feedback";

if (!mysqli_query($con, $sql)) 
{
die('Error: ' . mysqli_errno($con));
} 

$result= mysqli_query($con, $sql);
$thispage = $_SERVER['PHP_SELF'];


echo "<form method='post'action= $thispage>";




echo "<table border = '1' align='center'>
<br><br>
<tr><th>S.No.</th><th><input type = 'Submit' name = 'Submit' value = 'Delete'  ></th>
<th>Name</th> <th>Email</th><th>Address</th><th>Country</th><th>City</th><th>Zip Code</th><th>Phone</th><th>Mobile</th><th>Message</th>
</tr>";
 
while ($row = mysqli_fetch_array($result))
{
 
 echo "<tr>";
 echo "<td>" . $row['f_id'] . "</td>";
 echo "<td><input type = 'checkbox' name = 'chk' value = '1'  ></td>";
 echo "<td>" . $row['Name'] . "</td>";
 echo "<td>" . $row['Email'] . "</td>";
 echo "<td>" . $row['Address'] . "</td>";
 echo "<td>" . $row['Country'] . "</td>";
 echo "<td>" . $row['City'] . "</td>";
 echo "<td>" . $row['Zip'] . "</td>";
 echo "<td>" . $row['Phone'] . "</td>";
 echo "<td>" . $row['Mobile'] . "</td>";
 echo "<td>" . $row['Message'] . "</td>";

 echo "</tr>";
}

echo "</table>";
mysqli_close($con);

// echo $thispage;

if( $_POST['Submit'] = 'Delete')
{
  
  $con1 =  mysqli_connect("localhost", "root", "admin","paras"); 

   if (!$con1)
   {
     die('Could not connect: ' . mysqli_errno($con1));
   }
 
   $sql1="select f_id from feedback";

  if (!mysqli_query($con1, $sql1)) 
  {
   die('Error: ' . mysqli_errno($con1));
  } 
  $n = mysqli_num_rows($result1);
  // echo $n;
  for ($j = 0; $j<=$n; $j++)
  {  
    $c[$j] = $_POST['chk'];
    // echo $c[$j];
  
    
    if ($result1 = mysqli_query($con1, $sql1))
    {
        // $i = $j;
        while ($row1 = mysqli_fetch_array($result1))
        {
         $a[$j] = $row1[0];
        // echo $a[$j]; 
       // echo $row1[1];
          
           // echo $a[0];
            if ($c[$j] == 1)
            {
            
            echo "<input type= 'text' name = 't' value = ''>"; 
            echo "<br>";
          //echo $row1[$i];
            $d = "delete from feedback where f_id = '$a[$j]'";
            mysqli_query($con1, $d);

           }          
         }
        // $i++;
       }
     }
}
echo "</form>";
mysqli_close($con1);
?>

Posted: Mon Jan 22, 2007 8:48 am
by feyd
  • The following is an assignment. You want a equality check: ==

    Code: Select all

    $_POST['Submit'] = 'Delete'
    Also, it should be noted that it's often not recommended to look for a submit button in your submission processing as it's possible to submit forms without using the button.
  • Do not use $_SERVER['PHP_SELF'], it contains user input. Use "#" in this case instead.
  • When your code is displaying text input boxes named "t," it is deleted those records.
  • Your code can produce any number of check boxes. Each is named the same and contains the same value in your form. How do you know which record to delete? If multiple check boxes are selected, only the last will be seen by your script. This is due to your choice in naming the field "chk" as opposed to "chk[]" or similar.

Posted: Wed Jan 24, 2007 12:53 am
by shwetarani2002
feyd wrote:
  • The following is an assignment. You want a equality check: ==$_POST['Submit'] = 'Delete'
    Also, it should be noted that it's often not recommended to look for a submit button in your submission processing as it's possible to submit forms without using the button.
  • Do not use $_SERVER['PHP_SELF'], it contains user input. Use "#" in this case instead.
  • When your code is displaying text input boxes named "t," it is deleted those records.
  • Your code can produce any number of check boxes. Each is named the same and contains the same value in your form. How do you know which record to delete? If multiple check boxes are selected, only the last will be seen by your script. This is due to your choice in naming the field "chk" as opposed to "chk[]" or similar.
hi,
i make all changes as you said.but when i check any checkbox all checkboxes are selected automatically and following codes are not execute.

Code: Select all

if($c[$j] == on)
           {
             echo "<input type= 'text' name = 't' >"; 
             echo "<br>";
            //echo $row1[$i];
           $d = "delete from feedback where f_id = '$a[$j]'";
             mysqli_query($con1, $d);
          }
here is my full code

Code: Select all

<?php 

$con =  mysqli_connect("localhost", "root", "admin","paras"); 

if (!$con)
  {
  die('Could not connect: ' . mysqli_errno($con));
  }

$sql="select Name, Email, Address, Country, City, Zip, Phone, Mobile, Message,f_id from feedback";

if (!mysqli_query($con, $sql)) 
{
die('Error: ' . mysqli_errno($con));
} 

$result= mysqli_query($con, $sql);
$thispage = $_SERVER['PHP_SELF'];


echo "<form method='post'action= $thispage>";




echo "<table border = '1' align='center'>
<br><br>
<tr><th>S.No.</th><th><input type = 'Submit' name = 'Submit' value = 'Delete'  ></th>
<th>Name</th> <th>Email</th><th>Address</th><th>Country</th><th>City</th><th>Zip 

Code</th><th>Phone</th><th>Mobile</th><th>Message</th>
</tr>";
 
while ($row = mysqli_fetch_array($result))
{

 echo "<tr>";
 echo "<td>" . $row['f_id'] . "</td>";
 echo "<td><input type = 'checkbox' name = 'chk[]' value= 'off'   ></td>";
 echo "<td>" . $row['Name'] . "</td>";
 echo "<td>" . $row['Email'] . "</td>";
 echo "<td>" . $row['Address'] . "</td>";
 echo "<td>" . $row['Country'] . "</td>";
 echo "<td>" . $row['City'] . "</td>";
 echo "<td>" . $row['Zip'] . "</td>";
 echo "<td>" . $row['Phone'] . "</td>";
 echo "<td>" . $row['Mobile'] . "</td>";
 echo "<td>" . $row['Message'] . "</td>";

 echo "</tr>";
}

echo "</table>";
mysqli_close($con);

// echo $thispage;
$con1 =  mysqli_connect("localhost", "root", "admin","paras"); 

   if (!$con1)
   {
     die('Could not connect: ' . mysqli_errno($con1));
   }
 
   $sql1="select f_id from feedback";

  if (!mysqli_query($con1, $sql1)) 
  {
   die('Error: ' . mysqli_errno($con1));
  } 
  $result1 = mysqli_query($con1, $sql1);
  $n = mysqli_num_rows($result1);

if($_POST['Submit'] == 'Delete')
{
  
  
     echo $n;
 
  for ($j = 0; $j<$n; $j++)
  {  
    
    $c[$j] = $_POST['chk'];
    
    echo $c[$j];
  
    
    // $i = $j;
     while ($row1 = mysqli_fetch_row($result1))
      {
         $a[$j] = $row1[0];
         echo $a[$j]; 
          
           if($c[$j] == on)
           {
             echo "<input type= 'text' name = 't' >"; 
             echo "<br>";
            //echo $row1[$i];
           $d = "delete from feedback where f_id = '$a[$j]'";
             mysqli_query($con1, $d);
          }
                   
         }
        // $i++;
       }
    
}
echo "</form>";
mysqli_close($con1);
?>
please help me

Posted: Wed Jan 24, 2007 9:07 am
by feyd
$_POST['chk'] will be an array. The elements will not contain any specific information to associate them with an ID as you are using "off" as their value. "on" will not be any of the elements.