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
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;

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.