Jump to content

delete from db...some really bad code


crmamx

Recommended Posts

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
Share on other sites

<!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
Share on other sites

<!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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.