ant080808 Posted June 8, 2010 Share Posted June 8, 2010 Hi, I have been following a tutorial on php and mysql and have been given the below code to input and test. The records show up fine in my browser and I can add new records OK, but my problem is I can't delete any records. When I click delete it just refreshes the page but the data that I wanted to delete is still there. I don't get any sort of error either. Anyone have any idea what I'm doing wrong? Thanks Ant080808 <?php // sqltest.php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database, $db_server) or die("Unable to select database: " . mysql_error()); if (isset($_POST['author']) && isset($_POST['title']) && isset($_POST['type']) && isset($_POST['year']) && isset($_POST['isbn'])) { $author = get_post('author'); $title = get_post('title'); $type = get_post('type'); $year = get_post('year'); $isbn = get_post('isbn'); if (isset($_POST['delete']) && $isbn != "") { $query = "DELETE FROM classics WHERE isbn='$isbn'"; if (!mysql_query($query, $db_server)) echo "DELETE failed: $query<br />" . mysql_error() . "<br /><br />"; } else { $query = "INSERT INTO classics VALUES" . "('$author', '$title', '$type', '$year', '$isbn')"; if (!mysql_query($query, $db_server)) echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />"; } } echo <<<_END <form action="sqltest.php" method="post"><pre> Author <input type="text" name="author" /> Title <input type="text" name="title" /> Category <input type="text" name="type" /> Year <input type="text" name="year" /> ISBN <input type="text" name="isbn" /> <input type="submit" value="ADD RECORD" /> </pre></form> _END; $query = "SELECT * FROM classics"; $result = mysql_query($query); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j) { $row = mysql_fetch_row($result); echo <<<_END <pre> Author $row[0] Title $row[1] Type $row[2] Year $row[3] ISBN $row[4] </pre> <form action="sqltest.php" method="post"> <input type="hidden" name="delete" value="yes" /> <input type="hidden" name="isbn" value="$row[4]" /> <input type="submit" value="DELETE RECORD" /></form> _END; } mysql_close($db_server); function get_post($var) { return mysql_real_escape_string($_POST[$var]); } ?> Link to comment https://forums.phpfreaks.com/topic/204221-mysql-deleting-record-help/ Share on other sites More sharing options...
fenway Posted June 8, 2010 Share Posted June 8, 2010 Sounds like you need to trace your code. Link to comment https://forums.phpfreaks.com/topic/204221-mysql-deleting-record-help/#findComment-1069653 Share on other sites More sharing options...
ant080808 Posted June 9, 2010 Author Share Posted June 9, 2010 Sounds like you need to trace your code. Could someone explain how I would do this? I have only just started to learn PHP and this has not been explained in the tutorial I am following yet. Thanks Link to comment https://forums.phpfreaks.com/topic/204221-mysql-deleting-record-help/#findComment-1070024 Share on other sites More sharing options...
ant080808 Posted June 15, 2010 Author Share Posted June 15, 2010 Bump Has no one really got any idea why this code won't work? I only want to delete the record based on the ISBN number. I want to carry on learning php but can't until I get past this and understand why it won't work?? Link to comment https://forums.phpfreaks.com/topic/204221-mysql-deleting-record-help/#findComment-1072545 Share on other sites More sharing options...
TOA Posted June 15, 2010 Share Posted June 15, 2010 Bump Has no one really got any idea why this code won't work? I only want to delete the record based on the ISBN number. I want to carry on learning php but can't until I get past this and understand why it won't work?? The correct syntax to delete something in mysql is DROP **EDIT My bad, didn't read what you were trying to do correctly....ignore this post sorry Link to comment https://forums.phpfreaks.com/topic/204221-mysql-deleting-record-help/#findComment-1072549 Share on other sites More sharing options...
TOA Posted June 15, 2010 Share Posted June 15, 2010 First try to echo all your $_POST data to make sure it's coming through Link to comment https://forums.phpfreaks.com/topic/204221-mysql-deleting-record-help/#findComment-1072553 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.