Jump to content


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


Checking to see if name and emailadress already exists

Recommended Posts

Hi everybody, I'm a real noob at php but i try to muddle through as best I can.
Recently I had cause to make a webpage that includes a petition on it and i set it ut up like this.

I have a webpage with multiple form fields on it, one for name, address, email and comment.
I use php to connect to a mysql db that has all those fields in it and the script the checks if name and email exists, if they dont it inserts the form fields to a new entry, if they do exist it redirects to another page.

My problem is I seem to mess up the checking of the two fields part. I thought I'd been clever and set it up so that it checks if both fields exits in a given row but users can not add just a name without and email adress.

Any help here would be great, just keep in mind I am fairly new to php.

I will include my code here:


include "./scripts/config.php";
include "./scripts/opendb.php";

$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname,$conn) or die("Could not connect to db");

$navn = $_POST['navn'];
$adresse = $_POST['adresse'];
$email = $_POST['email'];
$kommentar = $_POST['kommentar'];

$query = "SELECT * FROM $table WHERE navn = '$navn' OR email = '$email'";
$result = mysql_query($query);
$numR = mysql_num_rows($result);

if($numR >= 1) {
header('Location: [a href=\"http://www.somesite.com/denied.html');\" target=\"_blank\"]http://www.somesite.com/denied.html');[/a]
else {
$query = "INSERT INTO $table (navn,adresse,email,kommentar) VALUES ('$navn','$adresse','$email','$kommentar')";
mysql_query($query,$conn) or die('Error, insert query failed'.mysql_error());

header('Location: [a href=\"http://www.somesite.com/thankyou.html');\" target=\"_blank\"]http://www.somesite.com/thankyou.html');[/a]

After this i have another else statement that prints the form and such and after that there is a ?>
So the code analyzer in zend studio tells me it should be well formatted.


Share this post

Link to post
Share on other sites
Well, you're using an OR -- sounds like you want both fields to be checked together.

Share this post

Link to post
Share on other sites


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.