JTGrime Posted August 26, 2010 Share Posted August 26, 2010 Hi guys can any one see hy tis won't work <?php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) { die ("Unabl to connect to MySQL" . mysql_error()); } mysql_select_db($db_database, $db_server) or die("Unable to connect to database: " . mysql_error()); if (isset($_POST['author'])&& isset($_POST['title'])&& isset($_POST['category'])&& isset($_POST['year'])&& isset($_POST['isbn'])) { $author = get_post('author'); $title = get_post('title'); $category = get_post('category'); $year = get_post('year'); $isbn = get_post('isbn'); if (isset($_POST['delete']) && $isbn != "") { $query = "DELETE FROM tblCLassics WHERE isbn='$isbn'"; if (!mysql_query($query, $db_server)) { echo "DELETE failed: $query<br />" . mysql_error() . "<br /><br />"; } } else { $query = "INSERT INTO tblClassics VALUES" . "('$author','$title','$category','$year','$isbn')"; if (!mysql_query($query, $db_server)) { echo "INSERT Failed: $query<br />" . mysql_error() . "<br /><br />"; } } } echo <<<_END <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome to the Book Club</title> </head> <body> <form action="connectionTest.php" method="post"><pre> Author: <input type="text" name="author" /> Title: <input type="text" name="title" /> Category: <input type="text" name="category" /> Year: <input type="text" name="year" /> ISBN: <input type="text" name="isbn" /> <input type="submit" value="ADD RECORD" /></pre> </form> _END; $query = "SELECT * FROM tblClassics"; $results = mysql_query($query); if (!$results) { die("Database access failed: " . mysql_error()); } $rows = mysql_num_rows($results); for ($j = 0; $j < $rows ; ++$j) { $row = mysql_fetch_row($results); echo <<<_END <pre> Author: $row[0] Title: $row[1] Category: $row[2] Year: $row[3] ISBN: $row[4] </pre> <form action="connectionTest.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; } echo "</body>"; echo "</html>"; mysql_close($db_server); function get_post($var) { return mysql_real_escape_string($_POST[$var]); } ?> cheers. Quote Link to comment https://forums.phpfreaks.com/topic/211823-cant-get-delete-query-to-work/ Share on other sites More sharing options...
Alex Posted August 26, 2010 Share Posted August 26, 2010 Which part isn't working? What's the output you get? Any errors? You haven't provided enough information. Quote Link to comment https://forums.phpfreaks.com/topic/211823-cant-get-delete-query-to-work/#findComment-1104120 Share on other sites More sharing options...
JTGrime Posted August 26, 2010 Author Share Posted August 26, 2010 sorry, no errors at all. it all works fine it just doesn't update the database when I delete a record. I have tried the query directly in MySQL monitor(that's the command line tool right?) and it deletes records from their but for some reason it wont do when I try to delete via the browser. WAMP 2.0 set up using local host. Quote Link to comment https://forums.phpfreaks.com/topic/211823-cant-get-delete-query-to-work/#findComment-1104134 Share on other sites More sharing options...
Alex Posted August 26, 2010 Share Posted August 26, 2010 Are you sure it's even getting to the part where the query is executed? If it is, you should print out of the query to make sure it contains what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/211823-cant-get-delete-query-to-work/#findComment-1104136 Share on other sites More sharing options...
PFMaBiSmAd Posted August 26, 2010 Share Posted August 26, 2010 if (isset($_POST['author'])&& isset($_POST['title'])&& isset($_POST['category'])&& isset($_POST['year'])&& isset($_POST['isbn'])) Your 'delete' forms only set the 'isbn' and 'delete' fields, so it will be a little hard for the above ^^^^ code to ever be TRUE. Quote Link to comment https://forums.phpfreaks.com/topic/211823-cant-get-delete-query-to-work/#findComment-1104138 Share on other sites More sharing options...
JTGrime Posted August 26, 2010 Author Share Posted August 26, 2010 if (isset($_POST['author'])&& isset($_POST['title'])&& isset($_POST['category'])&& isset($_POST['year'])&& isset($_POST['isbn'])) Your 'delete' forms only set the 'isbn' and 'delete' fields, so it will be a little hard for the above ^^^^ code to ever be TRUE. OK I got this out of a book as I'm learning and I am fairly unsure how to fix this issue. are you saying I should add something to the above snippet you highlighted? Quote Link to comment https://forums.phpfreaks.com/topic/211823-cant-get-delete-query-to-work/#findComment-1104140 Share on other sites More sharing options...
PFMaBiSmAd Posted August 26, 2010 Share Posted August 26, 2010 You would need to separate the logic for the 'delete' operation from the logic for the 'insert' operation. You apparently took some 'insert' code and added a delete operation inside of it. Quote Link to comment https://forums.phpfreaks.com/topic/211823-cant-get-delete-query-to-work/#findComment-1104147 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.