Jump to content

[SOLVED] deleting database entries


Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/57161-solved-deleting-database-entries/
Share on other sites

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;

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.