Jump to content

DELETE from table - Help plzzz


princeofpersia

Recommended Posts

Hi guys, I have a table of messeges where listed from database. I have added a column (thanks to php freak forum) where users can delete msgs from the table, there are two pages, view messege page which lists the messeges and a delete function page (please see below)

 

All the connections are fine and pagination works perfectly, but when i press on delete it wont delete anything from the database.

 

I have worked around it abit but still not working. Could you please help me to see what i am doing wrong here? im sure mysql is fine, surely there is something wrong im doing in my php

 

many thanks and merry xmas in advance.

 

 

 

 

View Messeges page:

 

<?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;
}
//get pageinatio in each page
$per_page=5;
/*
if( intval($_GET['start']) < 1 ) {
     $start = 0;
} else {
     $start = (int) $_GET['start'];
}
*/

$start=$_GET['start'];
$record_count =mysql_num_rows(mysql_query("SELECT * FROM msg"));


$max_page=$record_count/$per_page;

if(!$start)
$start=0;

$result= mysql_query("SELECT * FROM msg LIMIT $start, $per_page");

while($row = mysql_fetch_assoc($result))
{

$title=$row['title'];
$id_rent=$row['id_msg'];
?>
<table>


<tr>

	<td><? echo $id_msg; ?></td>
        <td><? echo $title; ?></td>
      

        		<td><?php echo "<a href='delete-msg.php?msg={$row['id_msg']}'>" ?>Delete</a></td></tr>
        </tr>

</table>
	<?php

	//while
	} 



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



if(!($start<=0))	
echo	"<a href='view-msgs?start=$prev'>Previous</a>";

if(!($start>=$record_count-$per_page))
echo	"<a href='view-msgs.php?start=$next'>Next</a>";	
mysql_close();
?>

 

 

 

delete-msg.php  DELETE function page:


<?php

include ("../global.php");

$id=$_GET['id_msg'];


$result=mysql_query("DELETE FROM msg WHERE id_msg='$id'");

if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='view-msg.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection
mysql_close();


?>

Link to comment
Share on other sites

Try this:

delete-msg.php

<?php
include ("../global.php");

$id=$_GET['msg'];
//echo $id;
if ($id != '' ) {
$result=mysql_query("DELETE FROM msg WHERE id_msg='$id'");
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='view-msg.php'>Back to main page</a>";
}
else {
echo "ERROR: Cannot delete the msg.";
}
}
else
{
echo "Msg id is empty";
}
// close connection
mysql_close();

?>

 

Tell us what you get?

Link to comment
Share on other sites

<?php

include ("../global.php");

$id=$_GET['msg'];
//echo $id;
if ($id != '' ) {
$result=mysql_query("DELETE FROM rent WHERE id_msg='$id'");


[code=php:0]
<?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;
}
//get pageinatio in each page
$per_page=5;


$start=$_GET['start'];
$record_count =mysql_num_rows(mysql_query("SELECT * FROM msg"));


$max_page=$record_count/$per_page;

if(!$start)
$start=0;

$result= mysql_query("SELECT * FROM msg LIMIT $start, $per_page");

while($row = mysql_fetch_assoc($result))
{


$title=$row['title'];
$id_rent=$row['id_msg'];
?>
<table>

<tr>

<td><? echo $id_msg; ?></td>
        <td><? echo $title; ?></td>
      

<td><?php echo "<a href='delete-msg.php?msg=$id_rent'>" ?>Delete</a></td></tr>
        </tr>

</table>





<?php










//while





} 



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













if(!($start<=0))






echo



"<a href='view-msgs?start=$prev'>Previous</a>";




if(!($start>=$record_count-$per_page))



echo



"<a href='view-msgs.php?start=$next'>Next</a>";




mysql_close();
?>


 

<?php

 

include ("../global.php");

 

$id=$_GET['msg'];

//echo $id;

if ($id != '' ) {

$result=mysql_query("DELETE FROM msg WHERE id_msg='$id'");

 

 

if($result){

 

 

 

echo "Deleted Successfully";

 

 

 

echo "<BR>";

 

 

 

echo "<a href='view-msg.php'>Back to main page</a>";

 

 

 

}

 

 

 

else {

 

 

 

echo "ERROR: Cannot delete the msg.";

 

 

 

}

}

else

{

 

 

 

echo "Msg id is empty";

}[/code]

Link to comment
Share on other sites

yeah, i have them in table along with pagination and it works perfectly

But i didnot see $id_msg variable any where in your code, how you get it from db.

 

Actually your code is messed up, you need to clean it according to your database table columns and echo correct variables.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.