Jump to content

simple else statement


wkilc

Recommended Posts

I'm usng this simple script to remove a row from a MySQL table:

 

<?php

$id = "123456789";
$pass = "bach";
$query = "DELETE FROM my_form WHERE member = ('$id') && password = ('$pass')";
$result = mysql_query($query);
echo "The data has been deleted.";

?>

 

Works as intended, if the password and ID match, then the row is deleted and it echos "The data has been deleted."  I want to add an "else" statement...  the the password and ID did not match, "the data has NOT been deleted".  I know I can't use "else" if I don' begin with an "if"... but I'm not sure where to put it (after the query?)

 

Thank you.

Link to comment
Share on other sites

Thank you.

 

Now I get: "Number of records deleted: 0" or "number of records deleted: 1"

 

This is good, but I was hoping for a unique response for incorrect ID and password combinations.

 

I'm a noob... so I'm starting simple.  Once I got this working... I'm going to modify this code so that the user inputs his/her ID and password using a simple form... then grab the form value form that POST and either delete the row or have it return an error.

 

<?php

$id = "123456789";
$pass = "bach";
$query = "DELETE FROM my_form WHERE member = ('$id') && password = ('$pass')";
$result = mysql_query($query);
printf ("Number of records deleted: %d\n", mysql_affected_rows());

?>

Link to comment
Share on other sites

Try

 

<?php

$id = "123456789";
$pass = "bach";
$query = "DELETE FROM my_form WHERE member = ('$id') && password = ('$pass')";
$result = mysql_query($query);
$affected = mysql_affected_rows();

if($affected == 0)
{
    //nothing was deleted
}
else
{
   //something was deleted
}

?>

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.