Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rows are not being deleted

Tags:

php

mysql

Here is my php code to select and delete rows form table:

    <h1>Deleting Multiple Records using PHP &amp; MySQL</h1>
     <p>&nbsp;</p>
     <?php

These are my db connection:

  $host = "localhost"; // Host name
 $username = "root"; // Mysql username
 $password = ""; // Mysql password
 $db_name = "project"; // Changed database name
 $tbl_name = "users";
 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);
?>

Here is my html code to retrieve data from database:

   <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>First Name</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Gender</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Experiance</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>User Name</strong></td>
            <td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
            </tr>
            <?php while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>

                <tr>
                <td align="center" bgcolor="#FFFFFF">
                    <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['fname']; ?></td>
                <td bgcolor="#FFFFFF"><?php echo $rows['lname']; ?>   </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['gend']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['phone']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['exp']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['uname']; ?>    </td>
                <td bgcolor="#FFFFFF"><?php echo $rows['pwd']; ?>    </td>
                </tr>
            <?php } ?>
            <tr><td colspan="5" align="center" bgcolor="#FFFFFF">
                <input name="delete" type="submit" id="delete" value="Delete" onclick="javascript:confirm('Are you sure to delete')"></td></tr>
            <?php
            if(isset($_POST['delete'])){
                $checkbox = $_POST['checkbox'];
              for($i=0;$i<count($_POST['checkbox']);$i++){
           $del_id = $checkbox[$i];
            $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
             print $sql;
             $result = mysql_query($sql);}

               if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";}}
     mysql_close();
    ?>
    </table></form></td></tr></table>
     <p>Record count: <?php echo number_format($count) ?></p>*/

I tried many times but it not deleting a single row from table. just now i changed the code of executing query....please check it and inform me...

like image 258
Ganesh Avatar asked Dec 03 '25 18:12

Ganesh


1 Answers

What is this query

$sql = "DEFRE FROM users WHERE id in (" . implode(",", $deleteIds) . ") ";

??

For deleting you should do something like this

$sql = "DELETE FROM users WHERE id IN (" . implode(",", $deleteIds) . ") ";
like image 66
chandresh_cool Avatar answered Dec 06 '25 06:12

chandresh_cool



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!