Page 1 of 1
Delete a record from a row in php
Posted: Tue Jun 17, 2008 9:14 pm
by stonelord
Hi, I have a webpage displaying contact info from a mysql DB, and now I added a Delete Record link so that each record can be deleted if need to; But it's not working and need some help. This is my delete link;
Code: Select all
echo '</td><td>';
echo '<a href="delete_contact.php">Delete Record</a>';
echo '</td></tr>';
This is my delete_contact.php code;
Code: Select all
<?
//create variables
$id=$_POST['delete'];
//connect to DB and insert data
mysql_connect("localhost", "ci_rod", "rod") or die('I cannot connect to data base because: '.mysql_error());
mysql_select_db("ci_testing") or die(mysql_error());
mysql_query("DELETE FROM `ci_testing` WHERE id=$id");
Print "Your information has been successfully deleted from the database.";
echo '<center>';
echo '<p><a href="phonelist.php">Back to Phone List</a></p>';
echo '</center>';
?>
Thank you
Re: Delete a record from a row in php
Posted: Tue Jun 17, 2008 9:18 pm
by John Cartwright
Going to need to be much more descriptive than "doesn't work"

Re: Delete a record from a row in php
Posted: Tue Jun 17, 2008 9:20 pm
by stonelord
Thank you for your comment, but would you mind telling me how descriptive? I am very new to programming and I am just trying new things. Please tell me if I should load my php files here. Thank you
Re: Delete a record from a row in php
Posted: Tue Jun 17, 2008 10:16 pm
by stonelord
Is this a very basic question? Can anyone please refer me to a 'basic php questions forum' if there is one? I'm just trying to learn, thank you
Re: Delete a record from a row in php
Posted: Tue Jun 17, 2008 10:26 pm
by John Cartwright
Elaborate on whats happening. Errors? Output?
Re: Delete a record from a row in php
Posted: Tue Jun 17, 2008 11:06 pm
by stonelord
I added an "OR die" and I get this error message;
Code: Select all
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Delete Record' at line 1
Re: Delete a record from a row in php
Posted: Tue Jun 17, 2008 11:17 pm
by John Cartwright
Ugh.. not my day. I had thought you had the mysql_error() reporting on. Your $id variable contains a value of "Delete Record", although it is expecting an id number. Taking a second look at your code, you are using a link to open the delete page, although you are accessing the page through a standard link, therefore no POST is ever done.. and in turn no $_POST['delete'] is set. How you got the "Delete Record" value is beyond me. A couple things I may suggest,
Pass the id number through the link, which can get accessed as $_GET['id']
Code: Select all
echo '</td><td>';
echo '<a href="delete_contact.php?id='. $your_id_number .'">Delete Record</a>';
echo '</td></tr>';
Secondly, if you are expecting a number, cast it to a number to assure no invalid information is passed through the query.
Code: Select all
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
Re: Delete a record from a row in php
Posted: Wed Jun 18, 2008 12:11 am
by stonelord
OK, I GOT IT !!! You made me think, thank you! Here is what I did, although I don't understand it completely yet, but hope to as time goes by;
Code: Select all
echo '</td><td>';
echo '<a href="delete_contact.php?id='.$row['id'].'">Delete Record</a>';
echo '</td></tr>';
And this is what I did on my delete php code;
Code: Select all
<?PHP
//connect to DB and insert data
mysql_connect("localhost", "my_db", "db") or die('I cannot connect to data base because: '.mysql_error());
mysql_select_db("ci_testing") or die(mysql_error());
$id=$_GET['id'];
mysql_query("DELETE FROM `PhoneList` WHERE id='$id'") OR die(mysql_error());
echo "Your information has been successfully deleted from the database";
mysql_close()
?>