vraghav Posted February 16, 2013 Share Posted February 16, 2013 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/274558-not-able-to-delete/ Share on other sites More sharing options...
denno020 Posted February 16, 2013 Share Posted February 16, 2013 (edited) 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 February 16, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/274558-not-able-to-delete/#findComment-1412760 Share on other sites More sharing options...
vraghav Posted February 16, 2013 Author Share Posted February 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274558-not-able-to-delete/#findComment-1412763 Share on other sites More sharing options...
denno020 Posted February 16, 2013 Share Posted February 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274558-not-able-to-delete/#findComment-1412775 Share on other sites More sharing options...
PaulRyan Posted February 16, 2013 Share Posted February 16, 2013 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.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/274558-not-able-to-delete/#findComment-1412818 Share on other sites More sharing options...
AyKay47 Posted February 16, 2013 Share Posted February 16, 2013 (edited) 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 February 16, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/274558-not-able-to-delete/#findComment-1412820 Share on other sites More sharing options...
PaulRyan Posted February 16, 2013 Share Posted February 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274558-not-able-to-delete/#findComment-1412824 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.