princeofpersia Posted December 19, 2010 Share Posted December 19, 2010 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 https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/ Share on other sites More sharing options...
UQ13A Posted December 19, 2010 Share Posted December 19, 2010 change $id=$_GET['id_msg']; to $id=$_GET['msg']; Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149216 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 i did try but it didnt work, btw why using msg? i need to delete the row Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149218 Share on other sites More sharing options...
mmarif4u Posted December 19, 2010 Share Posted December 19, 2010 As UQ13A said, you need msg because you are passing msg in the URL not id_msg. Also try to echo it in the delete page to see either it is empty or not. $id=$_GET['msg']; echo $id; Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149219 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 i have tied to echo it but all it shows is "Deleted Successfully" nothing else Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149220 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 hi btw msg is the table name Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149222 Share on other sites More sharing options...
mmarif4u Posted December 19, 2010 Share Posted December 19, 2010 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 https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149223 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 it says Msg id is empty Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149224 Share on other sites More sharing options...
mmarif4u Posted December 19, 2010 Share Posted December 19, 2010 Ok, we got some clue now. Now go to this line: <td><?php echo "<a href='delete-msg.php?msg={$row['id_msg']}'>" ?>Delete</a></td></tr> and change it with: <td><?php echo "<a href='delete-msg.php?msg=$id_rent'>" ?>Delete</a></td></tr> Now try... Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149226 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 still get Msg id is empty Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149227 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 mmarif4u what do u think if we have the delete function in the same page and work it out with checkboxes, then we not passing anything cross page, what do u think? but again checkboxes are another senario, Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149228 Share on other sites More sharing options...
mmarif4u Posted December 19, 2010 Share Posted December 19, 2010 Well, every thing is possible. Can you show us your updated code. Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149229 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 <?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 https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149230 Share on other sites More sharing options...
mmarif4u Posted December 19, 2010 Share Posted December 19, 2010 In your main page you echo these: <td><?php echo $id_msg; ?></td> <td><?php echo $title; ?></td> Did the msg id is printed? Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149231 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 yeah, i have them in table along with pagination and it works perfectly Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149232 Share on other sites More sharing options...
mmarif4u Posted December 19, 2010 Share Posted December 19, 2010 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 https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149234 Share on other sites More sharing options...
princeofpersia Posted December 19, 2010 Author Share Posted December 19, 2010 i had this which was a copy of a tutorial <td><? echo $id_rent; ?></td> changed to <td><? echo $id_msg; ?></td> and its in my table view-msg.php Link to comment https://forums.phpfreaks.com/topic/222127-delete-from-table-help-plzzz/#findComment-1149244 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.