nineseveninteractive Posted June 25, 2007 Share Posted June 25, 2007 Hi I am having problems deleting entries from a database using the following code case "deletevehicle": // Delete the vehicle from the database. $deletevehicle = deleteVehicle($id); // Return to the vehicle list. $vehiclelist = selectSiteCars(0); $message = "Vacancy Deleted!"; $template = "vehiclelist.php"; break; the deleteVehicle function which is invoked is: function deleteVehicle($id) { global $conn; $id = sqlInjectionProtect($id); $sql = mysql_query("DELETE FROM site_cars WHERE site_cars.car_id = '$id'") or die(mysql_error()); return($sql); } which i thought would delete the entry assigned to the variable id. unfortunatley this is not the case and all i get is the deletion message but the entry is not deleted. I have tried searching for a solution to this on google etc but can't solve the problem, so if anyone has any ideas or could point me in the right direction then i would be very grateful. many thanks in advance Quote Link to comment Share on other sites More sharing options...
trq Posted June 25, 2007 Share Posted June 25, 2007 Firstly, your deleteVehicle function need only be... <?php function deleteVehicle($id) { $id = sqlInjectionProtect($id); return mysql_query("DELETE FROM site_cars WHERE site_cars.car_id = '$id'") or die(mysql_error()); } ?> In your calling code you then need to check for success. eg; <?php case "deletevehicle": if ($deletevehicle = deleteVehicle($id)) { $message = "Vacancy Deleted!"; } else { $message = "Something wen't wrong"; } // Return to the vehicle list. $vehiclelist = selectSiteCars(0); $template = "vehiclelist.php"; break; ?> Quote Link to comment Share on other sites More sharing options...
nineseveninteractive Posted June 26, 2007 Author Share Posted June 26, 2007 Thanks for the advice. I will try that out and see if i can get it working! Quote Link to comment Share on other sites More sharing options...
nineseveninteractive Posted June 26, 2007 Author Share Posted June 26, 2007 I'm still having problems getting the delete function working. I put some error reporting code inand now get the error: Notice: Undefined variable: id in /home/sites/nidd-recruitment.com/public_html/admin/index.php on line 204 Debug: DELETE FROM site_cars WHERE site_cars.car_id = '' Debug: 0 affected row(s) so does this mean that i havent defined the variable earlier in the script? would i need some thing like $id = $_GET['id'] to do this? sorry if i am being stupid but i'm a php newbie! thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted June 27, 2007 Share Posted June 27, 2007 so does this mean that i havent defined the variable earlier in the script? would i need some thing like $id = $_GET['id'] to do this? thanks Yes (or $_POST['id'] depending on how you pass the id) Quote Link to comment Share on other sites More sharing options...
nineseveninteractive Posted June 27, 2007 Author Share Posted June 27, 2007 thats sorted it. thanks very much for the help! 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.