crmamx Posted February 5, 2011 Share Posted February 5, 2011 I know this is bad code but I got it to work. I tried using only the delete statement but could find no way to see if the record existed. I had a heck of a time getting the number of rows (there can only be one unique id). And finally, I could not get the if/else to work. Would anyone care to show me how a pro would do it? Thanks <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <?php // delete airplane in database // Connect to database ===================================================== include("connect_db.php"); // retrieve form data from form2.html ========================================== $id = $_POST['id']; // Send query =========================================================== $query = "SELECT * FROM airplanes WHERE id='$id'"; if (!mysql_query($query)){ die('Error :' .mysql_error()); } $result = mysql_query ($query); $num = mysql_num_rows($result); // Check to see if record exists ============================================== if (mysql_num_rows($result) == 1) { mysql_query("DELETE FROM airplanes WHERE id='$id'"); print 'Airplane successfully deleted'; } // Record not in db ========================================================= if (mysql_num_rows($result) == 0) { print '<big style="color: red;"><span style="font-weight: bold;"> ERROR MESSAGE:</span></big> ID number not found'; echo "<br />"; echo "<br />"; print 'Use the BACK button on your browser to try again'; echo "<br />"; } echo "<br />"; echo '<p>Return to <a href="members_menu.html">Members Menu Here</a></p>'; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/226780-delete-from-dbsome-really-bad-code/ Share on other sites More sharing options...
brown2005 Posted February 5, 2011 Share Posted February 5, 2011 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <?php // delete airplane in database // Connect to database ===================================================== include("connect_db.php"); // retrieve form data from form2.html ========================================== $id = $_POST['id'] ? $_POST['id'] : ""; // Send query =========================================================== $query = mysql_query ("SELECT * FROM airplanes WHERE id='$id'"); if (!$query){ die('Error :' .mysql_error()); } $num = mysql_num_rows($query); // Check to see if record exists ============================================== if($num) > 0){ mysql_query("DELETE FROM airplanes WHERE id='$id'"); print 'Airplane successfully deleted'; }else{ print '<big style="color: red;"><span style="font-weight: bold;"> ERROR MESSAGE:</span></big> ID number not found'; echo "<br />"; echo "<br />"; print 'Use the BACK button on your browser to try again'; echo "<br />"; } echo "<br />"; echo '<p>Return to <a href="members_menu.html">Members Menu Here</a></p>'; ?> </body> </html> i am no pro, and maybe there is a better way, but thats how i would write it Link to comment https://forums.phpfreaks.com/topic/226780-delete-from-dbsome-really-bad-code/#findComment-1170226 Share on other sites More sharing options...
PaulRyan Posted February 5, 2011 Share Posted February 5, 2011 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <?PHP include('connect_db.php'); // Include DB connection if(isSet($_POST['id']) && is_int($_POST['id']) && $_POST['id'] >= 1) { $planeID = (int)$_POST['id']; $myQuery = "SELECT id FROM airplanes WHERE id=$planeID"; if($doQuery = mysql_query($myQuery)) { if(mysql_num_rows($doQuery)) { $delPlane = mysql_query("DELETE FROM airplanes WHERE id=$planeID"); if($delPlane) { echo 'Airplane has been deleted. <br>'; } else { echo 'Unable to delete the airplane from the database. <br>'; } } else { echo 'This query return no results: '.$myQuery.' <br>'; } } else { echo 'This query failed: '.$myQuery.' <br>'; } } else { echo 'There is no plane ID posted. <br>'; } ?> <p> Return to <a href="members_menu.html">Members Menu Here</a> </p> </body> </html> I'd do it like the above, uses more error checking... Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/226780-delete-from-dbsome-really-bad-code/#findComment-1170227 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.