dbk Posted May 15, 2010 Share Posted May 15, 2010 Hi I can't seem to delete from the database with the "DELETE RECORD" button, please help! It works fine when saving records to the database! CODE: <?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['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 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', '$category', '$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> <table border=0 color=white> <tr> <td>Author</td><td><input type="text" name="author" /></td> </tr> <tr> <td>Title</td><td><input type="text" name="title" /></td> </tr> <tr> <td>Category</td><td><input type="text" name="category" /></td> </tr> <tr> <td>Year</td><td><input type="text" name="year" /></td> </tr> <tr> <td>ISBN</td><td><input type="text" name="isbn" /></td> </tr> <tr> <td><input type="submit" value="ADD RECORD" /></td> </tr> </pre></form></table> _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 <form action="sqltest.php" method="post"><pre> <table border=0 cellspacing=5> <tr bgcolor=#ECECEC> <td width=100px>Author</td><td>:</td><td width=300px>$row[0]</td> </tr> <tr bgcolor=#ECECEC> <td>Title</td><td>:</td><td>$row[1]</td> </tr> <tr bgcolor=#ECECEC> <td>Category</td><td>:</td><td>$row[2]</td> </tr> <tr bgcolor=#ECECEC> <td>Year</td><td>:</td><td>$row[3]</td> </tr> <tr bgcolor=#ECECEC> <td>ISBN</td><td>:</td><td>$row[4]</td> </tr> <tr> <td></td> <td></td> <td> <input type="hidden" name="delete" value="yes" /> <input type="hidden" name="isbn" value="$row[4]" /> <input type="submit" value="DELETE RECORD" /> </td> </tr> </pre></table></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/201839-cant-delete-record-from-mysql-with-php/ Share on other sites More sharing options...
graham23s Posted May 15, 2010 Share Posted May 15, 2010 Hi Buddy, Try changing: <td><input type="submit" name="delEntry" value="DELETE RECORD" /></td> and if (isset($_POST['delEntry']) && $isbn != "") Graham Link to comment https://forums.phpfreaks.com/topic/201839-cant-delete-record-from-mysql-with-php/#findComment-1058662 Share on other sites More sharing options...
dbk Posted May 15, 2010 Author Share Posted May 15, 2010 Hi graham23s Thanks for the reply! But.. i't doesn't make any difference - nothing gets deleted The isbn is used as the "primary key" in the database! Link to comment https://forums.phpfreaks.com/topic/201839-cant-delete-record-from-mysql-with-php/#findComment-1058663 Share on other sites More sharing options...
dbk Posted May 15, 2010 Author Share Posted May 15, 2010 Is this really that complicated? Alternatives methods are also welcome! Link to comment https://forums.phpfreaks.com/topic/201839-cant-delete-record-from-mysql-with-php/#findComment-1058680 Share on other sites More sharing options...
jskywalker Posted May 15, 2010 Share Posted May 15, 2010 $isbn = get_post('isbn'); insert something like: echo "ISBN: $isbn"; jsut to verify that the correct value is in $isbn if (isset($_POST['delete']) && $isbn != ""){ Link to comment https://forums.phpfreaks.com/topic/201839-cant-delete-record-from-mysql-with-php/#findComment-1058688 Share on other sites More sharing options...
.Stealth Posted May 15, 2010 Share Posted May 15, 2010 Have you checked the database user is allowed to delete records? Link to comment https://forums.phpfreaks.com/topic/201839-cant-delete-record-from-mysql-with-php/#findComment-1058693 Share on other sites More sharing options...
dbk Posted May 15, 2010 Author Share Posted May 15, 2010 The user is allowed to delete records and it works fine direct in CMF. Link to comment https://forums.phpfreaks.com/topic/201839-cant-delete-record-from-mysql-with-php/#findComment-1058713 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.