zero_ZX Posted June 3, 2008 Share Posted June 3, 2008 Hi guys, i whish to create a function which deletes a row in my database. (as you might have already guessed i need to delete users) however i got pretty much stocked on this: <html> <body> <? //connect to mysql //change user and password to your mySQL name and password mysql_connect("*","*","*"); //select which database you want to edit mysql_select_db("users"); //If cmd is not hit if(!isset($cmd)) { //display all the news $result = mysql_query("select * from users"); //run the while loop that grabs all the news scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the enws $title=$r["username"];//take out the title $id=$r["id"];//take out the id echo "<a href='delete.php?cmd=delete&id=$id'>$username - Delete</a>"; echo "<br>"; } } ?> <? if($cmd=="delete") { $sql = "DELETE FROM news WHERE id=$id"; $result = mysql_query($sql); echo "Row deleted!"; } ?> </body> </html> any change that i can learn something from you guys? (sry for being "noob" im still trying to learn, but no articles in here, none at google) Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/ Share on other sites More sharing options...
imdead Posted June 3, 2008 Share Posted June 3, 2008 $sql = "DELETE FROM news WHERE id=$id"; shouldn't that be $sql = "DELETE FROM users WHERE id=$id"; Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-556834 Share on other sites More sharing options...
zero_ZX Posted June 3, 2008 Author Share Posted June 3, 2008 oh yea thats correct. However at line 18 ( while($r=mysql_fetch_array($result)) ) i get following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\nsa\delete.php on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-556841 Share on other sites More sharing options...
DarkWater Posted June 3, 2008 Share Posted June 3, 2008 $result = mysql_query($sql) OR die (mysql_error()); Change it to that. Also, I'm positively disgusted with the amount of times that this error comes up because people don't add a die clause to their queries. *Utter disappointment* Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-556843 Share on other sites More sharing options...
revraz Posted June 3, 2008 Share Posted June 3, 2008 Use mysql_error() after the query above it to see why your sql failed. Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-556845 Share on other sites More sharing options...
zero_ZX Posted June 3, 2008 Author Share Posted June 3, 2008 Query was empty Wtf? i mean im just replacing the $id with a number is that uncorrect? Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-556849 Share on other sites More sharing options...
DarkWater Posted June 3, 2008 Share Posted June 3, 2008 You never set $id. Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-556851 Share on other sites More sharing options...
revraz Posted June 3, 2008 Share Posted June 3, 2008 Your error may be coming from here $result = mysql_query("select * from users"); since that is related to line 18. Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-556870 Share on other sites More sharing options...
imdead Posted June 28, 2008 Share Posted June 28, 2008 <? if($cmd=="delete") { $id = $_GET['id']; $sql = "DELETE FROM news WHERE id=$id"; $result = mysql_query($sql); echo "Row deleted!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-576762 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.