Page 1 of 1

Check if data already exists in database

Posted: Sun Feb 24, 2008 11:00 am
by kevinks
Hello guys, its me again :p
Have been trying to insert a piece of code that will check if a someone is already a registered user.
So herez my piece of code:

<?php
include('functions.php');

if ($_POST['cust_name']=='' || alpha_numeric($_POST['cust_name'])==FALSE)
{$errors[] = 'A NAME is required. It must be alpha-numeric';}

if (strlen($_POST['cust_name'])<3)
{$errors[] = 'Name must contain 3 characters or more ';}

if ($_POST['cust_id']=='' || alpha_numeric($_POST['cust_id'])==FALSE)
{$errors[] = 'Customer ID is required and must be alpha-numeric';}

if (strlen($_POST['password'])<6)
{$errors[] = 'A password should be atleast 6 characters long';}

if ($_POST['password']=='' || alpha_numeric($_POST['password'])==FALSE)
{$errors[] = 'A password is required and must be alpha-numeric';}

if ($_POST['password']!=$_POST['re_Password'])
{$errors[] = 'The two passwords must match';}

if ($_POST['cust_nid']=='' || alpha_numeric($_POST['cust_nid'])==FALSE)
{$errors[] = 'A National ID is required and must be alpha-numeric';}

if ($_POST['cust_addr']=='' || alpha_numeric($_POST['cust_addr'])==FALSE)
{$errors[] = 'An Address is required and must be alpha-numeric';}

if ($_POST['cust_home_phone']=='' || $_POST['cust_mobile_phone']=='')
{$errors[] = 'A phone number is required';}


if (valid_email($_POST['cust_email'])==FALSE)
{$errors[] = 'Please supply a valid email address';}

if ($_POST['cust_crecard_no']=='' || alpha_numeric($_POST['cust_crecard_no'])==FALSE)
{$errors[] = 'A Credit Card number is required';}


if(is_array($errors))
{
echo '<p class="error"><b>The following errors occured</b></p>';
while (list($key,$value) = each($errors))
{

echo '<span class="error">'.$value.'</span><br />';
}
}
else {

$cust_id = $_POST['cust_id'];
$password = md5($_POST['password']);
$cust_title = $_POST['cust_title'];
$cust_name = $_POST['cust_name'];
$cust_nid = $_POST['cust_nid'];
$cust_addr = $_POST['cust_addr'];
$cust_district = $_POST['cust_district'];
$cust_home_phone = $_POST['cust_home_phone'];
$cust_mobile_phone = $_POST['cust_mobile_phone'];
$cust_cat = $_POST['cust_cat'];
$cust_email = $_POST['cust_email'];
$cust_crecard_no = $_POST['cust_crecard_no'];


include '../library/config.php';
include '../library/opendb.php';


$query = "INSERT INTO customer (cust_id, password, cust_title, cust_name, cust_nid, cust_addr, cust_district, cust_home_phone, cust_mobile_phone, cust_cat, cust_email, cust_crecard_no) VALUES ('$cust_id', '$password','$cust_title', '$cust_name', '$cust_nid', '$cust_addr', '$cust_district', '$cust_home_phone', '$cust_mobile_phone', '$cust_cat', '$cust_email', '$cust_crecard_no')";

mysql_query($query) or die('Customer ID already exist, please choose another one');
include '../library/closedb.php';

echo '<p><b>Success!</b></p>';
echo '<span>Your registration was successfully processed. You may login and start using your account. Thank you for registering !</span>';
}

?>


Can anyone tell me how i should modify the query part to accommodate the piece of code that will check if the user is already registered? Tried javascript, but cant get it to work properly :S
Thx

Re: Check if data already exists in database

Posted: Sun Feb 24, 2008 11:13 am
by LiveFree
Before the INSERT query, you could run something like this:

Code: Select all

 
$sql = "SELECT `cust_id` FROM `customer` WHERE `cust_id` = '" . $cust_id . "'";
$result = mysql_query($sql);
 
if (mysql_num_rows($result) == 1)) {
    //Already exists
} else {
    // Does not exists
}