Page 1 of 1

Simple Question

Posted: Sun Nov 08, 2009 7:57 am
by jefffan24
I have this script to add an email into my database:

Code: Select all

 
$clicked = $_POST['email'];
if ($clicked) {
$sql = "INSERT INTO emails (emails) VALUES ('$_POST[email]')";
mysql_query($sql);
 
$blah = "Thank You, your email has been added to our list.";
}
 
 
 
mysql_close($con);
It works fine and all, but I have my field set to unique in my database so if somebody enters in the same email address twice then it returns a default error on a plain white page. This is my form:

Code: Select all

 
<form method="post" name="blah" action="<?php echo $PHP_SELF; ?>">
<input type="text" size="30" name="email" id="email" class="ex">&nbsp;<input type="submit" value="submit" class="submit">
</form><?php echo $blah; ?>
As you can see I echo blah so that when they add their email address it gives them that message. I know there is a way but can somebody show me how to display a message that says: "That email is already in our database!" ?

Also can you show me how to use mysql_real_escape_string in this script? I tried putting it in and it would come up with the message thank you message, but it wasn't really entering the email address in.

Thanks alot in advance :D

Re: Simple Question

Posted: Sun Nov 08, 2009 8:13 am
by requinix
You can use mysql_affected_rows to see how many rows were "affected" (inserted) by the last query.

For mysql_real_escape_string,

Code: Select all

if (isset($_POST["email"])) { // use isset to check that the form was submitted
    $email = mysql_real_escape_string($_POST["email"]);
    $sql = "INSERT INTO emails (emails) VALUES ('$email')";
    mysql_query($sql);
 
    $blah = "Thank You, your email has been added to our list.";
}
Note the indentation: learn to do it.

Re: Simple Question

Posted: Sun Nov 08, 2009 8:16 am
by jefffan24
Thank you that worked for the escape string, I'll try that affected rows thing and let you know how that goes.

Ok so how do I use that mysql_affected_rows ? I can't seem to be able to get it to work.

Re: Simple Question

Posted: Mon Nov 09, 2009 1:24 pm
by jefffan24
Anybody else know how to do this? So that it displays a message saying "Sorry that email is already in our database" if their email is already in the database?

Code: Select all

 
<?php 
 
if (isset($_POST["email"])) { // use isset to check that the form was submitted
    $email = mysql_real_escape_string($_POST["email"]);
    $sql = "INSERT INTO emails (emails) VALUES ('$email')";
    mysql_query($sql);
 
    $blah = "Thank You, your email has been added to our list.";
}
/*
if (mysql_affected_rows() > 0){
    $error = "Test";
}*/
mysql_close($con);
 
?>
My failed attempt sort of. It showed $error all the time rather then just when their email was already in the db. This is how I'm calling $blah and $error,

Code: Select all

 
<center><h3>Get added to our email list:</h3>
<form method="post" name="blah" action="<?php echo $PHP_SELF; ?>">
<input type="text" size="30" name="email" id="email" class="ex">&nbsp;<input type="submit" value="submit" class="submit">
</form><?php echo $blah; ?><?php echo $error; ?></center>

Re: Simple Question

Posted: Mon Nov 09, 2009 4:26 pm
by requinix
If mysql_affected_rows returns zero then there were no rows inserted in the table.
If mysql_affected_rows returns one then there was one row inserted in the table.
If mysql_affected_rows returns two then there were two rows inserted in the table...

Re: Simple Question

Posted: Tue Nov 10, 2009 5:03 am
by jefffan24
I know that but how would I work that into my code? Right now it is either going to return a 0 or 1 b/c I have the field set to unique in my database. I only need one for when it is more then 0.

Re: Simple Question

Posted: Tue Nov 10, 2009 4:47 pm
by jefffan24
Nobody has anything? Sorry to be pushy its just the faster the better on this for me :/ I've tried everything I know and I can't get it to work.