Checking a Column

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
I made a script that will add new users to my db but I first want it to check the entire column to see if the username exists. Is there any specific function that would be good or should I just make a loop to go through each row?
 
I have a similar problem with checking the mysql database for existing records, but, I also want the users address to be stored in a separate table, as well as redirected to a certain page if the address already exists. If the address is not found with a records search, then the address is still logged and the visitor is redirected to a different page.

Here is what I have so far, any help would be appreciated.

PHP:
<?php
$username="xxxxx"; 
$password="xxxxxx"; 
$database="address"; 

mysql_connect("xxxxxxxx",$username,$password); 
mysql_select_db($database) or die( "Unable to select database"); 

$formaddress=$_POST['address'];

$selectQuery = db_query("SELECT address FROM addresses WHERE address='$formaddress'");

// see if there are any matches

if (db_numrows($selectQuery) == 1) {

// if a match occurs - update the addresslog table and send user to a specific page

db_query("INSERT INTO addresslog VALUES ('$formaddress')");

header("Location: http://www.thispage.com/");

} /// if no match, update addresslog table and send user to a different page

else {

db_query("INSERT INTO addresslog VALUES ('$formaddress')");

header("Location: http://www.otherpage.com/");

?>
 
I wouldn't use the address as a way of checking the data, since addresses can be miss-spelled etc. It would have to be exactly right for a match to occur. Better to pick one thing that won't change (like an email address) and use that to search for a matching record
 
Back
Top