contra10 Posted February 23, 2009 Share Posted February 23, 2009 hi i'm trying to allow my users to delete post while in a while statement heres my code <form action="" method="get" enctype="application/x-www-form-urlencoded"> <table border= "1" id="results"> <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); //This is your query again, the same one... the only difference is we add $max into it $data_p = "SELECT * FROM post_profile WHERE userid = '$idp' ORDER BY ppid DESC"; $posts = mysql_query($data_p) or die(mysql_error()); //This is where you display your query results while($info = mysql_fetch_array($posts)) { $userpost= "{$info['post']}"; $updel= "{$info['ppid']}"; $usernamep= "{$info['postingusername']}"; $userdate= "{$info['datepost']}"; $text = ($userpost); $newtext = wordwrap($text, 30, "<br />\n",true); if(isset($_POST['deletep'])){ $deletem= "DELETE FROM `post_profile` WHERE `ppid` = '$updel'"; $deletemessage = mysql_query($deletem) or die(mysql_error()); } echo"<tr><td></td></tr>"; echo" <tr><td>$newtext</td></tr>"; echo" <tr><td><FONT size='1'>Posted by $usernamep on $userdate</FONT></td><td>"; ?> <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'> <input type='submit' name='deletep' value='delete'> </form> <?php echo"</td></tr>"; } ?> Quote Link to comment Share on other sites More sharing options...
samshel Posted February 23, 2009 Share Posted February 23, 2009 1) u r querying user table with user id so there will be only one record, why do u need while loop? 2) if its not user table and somethign else and returns more than one record, then what u r doing is fetching all records, and deleting it one by one. anyways all ur records for that user are going to be deleted, why not just delete them qith one query. while loop not required again ! Quote Link to comment Share on other sites More sharing options...
contra10 Posted February 24, 2009 Author Share Posted February 24, 2009 the id in the query as you stated gets all the posts that have been stated towards that user...I have created the abilitiy for the user to erase all of his/her post. But how about if i wanted to allow the user to delete specific post? Quote Link to comment Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 you will have to take the post id also from the user, which u r not taking now. Quote Link to comment Share on other sites More sharing options...
contra10 Posted February 24, 2009 Author Share Posted February 24, 2009 $updel is the post id as the post id is withrawn from the query in the while satement, ...should the if statement be in the while brackets? Quote Link to comment Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 $updel is in while loop for all ids u r fetching from database. to delete specific post, you will have to take post id from user. 1) Show all posts to user using while loop. Each post will have a radio button. this should be ofcourse inside a form. 2) On submit, put value of radio button in $updel. dont do the while loop. just fire the delete query once with $updel and user_id (BTW where is user id coming from?) in the WHERE clause. Quote Link to comment Share on other sites More sharing options...
contra10 Posted February 24, 2009 Author Share Posted February 24, 2009 coming from url if(is_numeric($_GET['user'])){ $idp = $_GET['user']; } Quote Link to comment Share on other sites More sharing options...
samshel Posted February 24, 2009 Share Posted February 24, 2009 ok try to do step 1) Quote Link to comment Share on other sites More sharing options...
contra10 Posted February 24, 2009 Author Share Posted February 24, 2009 thnks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.