liamloveslearning Posted July 7, 2011 Share Posted July 7, 2011 Hi all, I have a table... ID user_id interest 1 20 football 2 20 tennis 3 20 soccer Im trying to write a function that when the user is logged in, his 3 interests are shown, he then has the option to delete an interest only I cant quite get it to work, can anybody see where im going wrong? Im still a novice with PHP so my code may appear very odd... <?php if(isset($_POST['int1'])) { $interest = $_POST['int1']; $qResult= mysql_query("DELETE " . mysql_real_escape_string($interest) . "FROM user_interests WHERE user_id = " . mysql_real_escape_string($usersClass->userID())); } elseif(isset($_POST['int2'])) { $interest = $_POST['int2']; $qResult= mysql_query("DELETE " . mysql_real_escape_string($interest) . "FROM user_interests WHERE user_id = " . mysql_real_escape_string($usersClass->userID())); } elseif(isset($_POST['int3'])) { $interest = $_POST['int3']; $qResult= mysql_query("DELETE " . mysql_real_escape_string($interest) . "FROM user_interests WHERE user_id = " . mysql_real_escape_string($usersClass->userID())); } print $interest1 . "<form method='post' action='#'><input type='hidden' value='".$interest1."' name='int1' id='int1'/><input type='submit' value='delete' /></form><br />"; print $interest2 . "<form method='post' action='#'><input type='hidden' value='".$interest2."' name='int2' id='int2'/><input type='submit' value='delete' /></form><br />"; print $interest3 . "<form method='post' action='#'><input type='hidden' value='".$interest3."' name='int3' id='int3'/><input type='submit' value='delete' /></form><br />"; ?>[/code[ Quote Link to comment https://forums.phpfreaks.com/topic/241369-delete-function-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2011 Share Posted July 7, 2011 You have no logic on there to report any errors returned from MySQL. That would be the first thing you should do, using mysql_error(). Quote Link to comment https://forums.phpfreaks.com/topic/241369-delete-function-not-working/#findComment-1239844 Share on other sites More sharing options...
liamloveslearning Posted July 7, 2011 Author Share Posted July 7, 2011 I'vr added or die() which now gives me this Could not delete data: Unknown column 'tenni' in 'where clause' The 'tenni' is a value though, I weant to delete the row that contains the value 'tenni' in the interests column Quote Link to comment https://forums.phpfreaks.com/topic/241369-delete-function-not-working/#findComment-1239848 Share on other sites More sharing options...
QuickOldCar Posted July 7, 2011 Share Posted July 7, 2011 is it tenni, or should it be tennis. Quote Link to comment https://forums.phpfreaks.com/topic/241369-delete-function-not-working/#findComment-1239852 Share on other sites More sharing options...
liamloveslearning Posted July 7, 2011 Author Share Posted July 7, 2011 Ive finally found the problem, Firstly I used 'or die()' to see where I was going wrong (I SHOUDL ALWAYS DO THIS!) I then received the error 'unknown column in where clause' which led me to add '' around my var, the final query is... $qResult= mysql_query("DELETE from user_interests WHERE interest = '" . mysql_real_escape_string($interest) . "' and user_id = " . $usersClass->userID()); if(! $qResult ) { die('Could not delete data: ' . mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/241369-delete-function-not-working/#findComment-1239854 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.