Jump to content

Not able to delete


vraghav

Recommended Posts

<?php $id = $_GET['id'];

 

//PHP MySQL Connection

 

//1. Create a database connection

 

mysql_connect("localhost", "root", "" ) or die("Could not connect to the server");

 

//2. Select a database to use

 

mysql_select_db("mproject") or die("Could not find the database");

 

 

//3. Perform database query

$sql = "DELETE FROM project_details WHERE id = $id";

$result = mysql_query($sql);

 

//4. Use returned data (if any)

 

//5. Close the database

mysql_close();

?>

 

I've used the above code to delete a single record. But am getting - a notice and its not deleting anything

 

Notice: Undefined index: id in C:\xampp\htdocs\xampp\SimpleCRUD\delete_record.php on line 3

 

 

Please assist on how to proceed.

Link to comment
Share on other sites

It looks like the ID that's being passed through the address bar (through the GET method) isn't working, or has been set as something else..

 

the link to use this script should be as follows:

 

<a href="delete_record.php?id=1">Delete Record</a>

 

That will pass the variable through to the script correctly, as I believe that's where you're having problems..

Edited by denno020
Link to comment
Share on other sites

Thanks LD,

 

That was helpful.... i changed the script "delete_record.php?id=$id" in the file (delete_customer.php) which calls delete_record.php - which deletes particular record, which I want to delete.

Now its working fine.

 

Just curious - Is there any way round to do this an alternate method?

 

Regards and Thanks

Raghav.

Link to comment
Share on other sites

I'm pretty sure the only other way you could do it is to use POST.. But that's a little more difficult to actually make happen because POST generally only works when a form is submitted. You could probably create a POST using javascript, but it's much easier to do as you're done and just pass the id through the address bar and retrieve using the GET method.

Link to comment
Share on other sites

This is how I would do it, it prevents injection from a rogue $_GET['id'] value.

You would have to add in some more detailed error messages and log any queries that don't execute.

 

<?PHP 

 mysql_connect('localhost', 'root', '') or die('Could not connect to the server');

 mysql_select_db('mproject') or die("Could not find the database");

 //### Type cast value for MySQL safety
 $id = (int)$_GET['id'];

 //### Check if the id is empty
 if(!empty($id)) {
   echo 'No ID selected.';
 } else {

   //### Create and execute query to delete project
   $deleteProjectQuery = "DELETE FROM `project_details` WHERE `id` = {$id}";
   $deleteProject      = mysql_query($deleteProjectQuery);

   //### Check if the MySQL query was executed successfully
   if(!$deleteProject) {
     echo 'Unable to delete selected project.';
   } else {
     echo 'Successfully deleted select project.';
   }
 }

?>

Link to comment
Share on other sites

make sure that the "id" index is set in the $_GET superglobal array before setting it's value to a variable.

 

$id = (isset($_GET["id"])) ? intval($_GET["id"]) : false

 

then proceed with PaulRyan's code

Edited by AyKay47
Link to comment
Share on other sites

make sure that the "id" index is set in the $_GET superglobal array before setting it's value to a variable.

 

$id = (isset($_GET["id"])) ? intval($_GET["id"]) : false

 

then proceed with PaulRyan's code

 

Just realised that I had error reporting turned off on my local dev setup. Not sure why, but it's back on, 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.