Jump to content


Photo

Checking to see if name and emailadress already exists


  • Please log in to reply
1 reply to this topic

#1 Caladan

Caladan
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 29 May 2006 - 10:49 PM

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:


<?php

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


if(isset($_POST['add']))
{
$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());

mysql_close();
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.

Caladan

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 30 May 2006 - 04:59 AM

Well, you're using an OR -- sounds like you want both fields to be checked together.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users