August 26, 201016 yr Hi Guys, Sorry to keep mitering like this can any one see a reason why this: <?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]); } ?> Won't actually delete from the database? cheers.
August 26, 201016 yr Does the query work in a Database admin app/phpMyAdmin or whatever you use? Does the mysql user have permission to delete on that table? What's the error it's returning?
August 26, 201016 yr Author Does the query work in a Database admin app/phpMyAdmin or whatever you use? Does the mysql user have permission to delete on that table? What's the error it's returning? good point yes I have admin access and I didn't actually check the query directly I just followed the book but I will use it now in MySQL monitor CMD LNE and see if it works, if not then I'll try DROP?
August 26, 201016 yr Author yes it works directly in the command line :/ so it has to be and issue with passing the command over to mysql right? but then none of my error messages appear so it must be getting there, because there is no die error.
August 26, 201016 yr The delete SQL command looks fine to me. An alternative to using delete would be to have a delete field on the table then mark a record as deleted with an update query. You'd then do the initial select something like this: SELECT * FROM table WHERE deleted = 0 That's the way I'd usually do it, but i find delete queries a bit scary
August 27, 201016 yr yes it works directly in the command line :/ so it has to be and issue with passing the command over to mysql right? but then none of my error messages appear so it must be getting there, because there is no die error. Sorry we cross posted last night and I missed your reply. Are you logging into the mysql with the same user/password combo as the script? If so I'd suggest popping a few echo's in there to see what's actually being executed.
Create an account or sign in to comment