Log in

View Full Version : little issue php script



Rosalie
10-04-2010, 08:55 PM
Hello everyone,
Okay, I tried;) to write this little script in php. I am obviously doing something wrong. I get the following error messages:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/rosalie/public_html/email.php on line 7

Warning: Cannot modify header information - headers already sent by (output started at /home/rosalie/public_html/email.php:7) in /home/rosalie/public_html/email.php on line 10

I want my script first to check whether the entered email address already excists in the database. When this isn't the case it should be added.

This is my script:


<?php

include('confform.php');

$query1="SELECT * FROM mailinglist WHERE email ='$email'";
$result1=mysql_query($query1);
$num1=mysql_num_rows($result1);

if($num1 != 0); {
header("Location:notregistered.html");
}


if($num1 == 0); {
$query2="INSERT INTO mailinglist SET email='$email'";
$result2=mysql_query($query2);
}

if($result2) {
header("Location:thankyou.html");
}
?>

Thanks in advance,
I hope to learn from it.

fastsol1
10-04-2010, 09:40 PM
Do this and see if it gives you any other errors, your code looks good to me.

$result1=mysql_query($query1) or die(mysql_error());

Rosalie
10-05-2010, 10:49 AM
Do this and see if it gives you any other errors, your code looks good to me.

$result1=mysql_query($query1) or die(mysql_error());

Thank you for taking the time to help me fastsol1. I don't have any error messages anymore and I can succesfully insert an email address.

However, I am still able to insert twice the same email address while this shouldn't be possible because of:


if($num1 != 0); {
header("Location:notregistered.html");
}

Does anyone know how I can solve this??

Thanks in advance

fastsol1
10-05-2010, 01:13 PM
actually I am surprised the code didn't throw more errors cause of this but you have ; right after your if() blocks which will end that line and not run the code under it. Take those out and it should work. Sorry I didn't catch that before I was only focused on the num_rows area.

Rosalie
10-05-2010, 01:38 PM
actually I am surprised the code didn't throw more errors cause of this but you have ; right after your if() blocks which will end that line and not run the code under it. Take those out and it should work. Sorry I didn't catch that before I was only focused on the num_rows area.

Thanks a lot!! I didn't knew that you shouldn't put ;. I really learned from this :)

traq
10-05-2010, 02:27 PM
the semicolon ends your line of code. so, if($num1 != 0); literally means "if num1 is not 0, do nothing."

Rosalie
10-05-2010, 04:39 PM
the semicolon ends your line of code. so, if($num1 != 0); literally means "if num1 is not 0, do nothing."

Thanks Adrien! I didn't knew that. I thought you had to place ; after every script line :o

I made almost all my php scripts with the help of tutorials. The problem is that I often don't really know what everything precisely means. Also when I want to change something it often takes me hours to figure out what causes errors. It is much better to be able to understand the script so that I know what I'm doing.