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
https://forums.phpfreaks.com/topic/245292-simple-else-statement/
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());

?>

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
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.