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) 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"; 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 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* 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. 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? 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. 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. 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!"; } ?> Link to comment https://forums.phpfreaks.com/topic/108585-delete-users/#findComment-576762 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.