Jump to content

PHP DELETE


princeofpersia

Recommended Posts

Hi,i know this is a duplicated thread but i have made changed to code and I have been trying to create a functionality which allows users to delete multiple entries from database, I have been working on this for a while now but cant get my head around it, can u please tell me what im doing wrong here?

 

thanks in advance

 

<?php

session_start();

include ("../global.php");
//welcome messaage
$username=$_SESSION['username'];
echo "$username";

$query=mysql_query("SELECT id FROM users WHERE username='$username'");
while($row = mysql_fetch_assoc($query))
{
$user_id = $row['id'];
echo $id;
}

$per_page=5;


$start=$_GET['start'];

$record_count =mysql_num_rows(mysql_query("SELECT * FROM rent"));
?>
<td><form name="form1" method="post" action=""> </td>
<?php
$max_page=$record_count/$per_page;

if(!$start)
$start=0;
$result= mysql_query("SELECT id, msg, title, reference FROM msg LIMIT $start, $per_page");
while($row = mysql_fetch_array($result))
{
            
$id=$row['id'];
$msg=$row['msg'];
$reference=$row['reference'];
$title=$row['title'];

?>



<table>


<tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
        <td><?php echo $title; ?></td>
        <td><?php echo $id; ?></td>
        <td><?php echo $reference; ?></td>
        <td><?php echo $msg; ?></td>


        </tr>
        
</table>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
        <?php
        
        //while
        }


$prev=$start - $per_page;
$next=$start + $per_page;        
        


if(!($start<=0))    
    echo    "<a href='view-data.php?start=$prev'>Previous</a>";
    
if(!($start>=$record_count-$per_page))
    echo    "<a href='view-data.php?start=$next'>Next</a>";    
if(isset($_POST['delete']))
{
    for($i = 0; $i <= count($_POST['checkbox']); $i++)
    {
        $del_id = $checkbox[$i];
        $sql = "DELETE FROM rent WHERE id='$del_id'";
        $result = mysql_query($sql);
    }
    

    if($result)
    {
        header('Location: view-data.php');
    }
}
?>

</table>
</form>

Link to comment
https://forums.phpfreaks.com/topic/222148-php-delete/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.