Delete a record from a row in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stonelord
Forum Newbie
Posts: 6
Joined: Tue Jun 17, 2008 8:48 pm

Delete a record from a row in php

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Delete a record from a row in php

Post by John Cartwright »

Going to need to be much more descriptive than "doesn't work" :wink:
stonelord
Forum Newbie
Posts: 6
Joined: Tue Jun 17, 2008 8:48 pm

Re: Delete a record from a row in php

Post 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
stonelord
Forum Newbie
Posts: 6
Joined: Tue Jun 17, 2008 8:48 pm

Re: Delete a record from a row in php

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Delete a record from a row in php

Post by John Cartwright »

Elaborate on whats happening. Errors? Output?
stonelord
Forum Newbie
Posts: 6
Joined: Tue Jun 17, 2008 8:48 pm

Re: Delete a record from a row in php

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Delete a record from a row in php

Post 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;
stonelord
Forum Newbie
Posts: 6
Joined: Tue Jun 17, 2008 8:48 pm

Re: Delete a record from a row in php

Post 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()
?>
Post Reply