GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Jun-2007, 18:52
ninedoors ninedoors is offline
New Member
 
Join Date: Jun 2007
Posts: 3
ninedoors is on a distinguished road

Trying to check the existance of data in MySQL


I am new to MySQL and PHP. I have created a simple form on my website that asks for the users name, email, subject and the ereason for contact. When the user hits the send button an email is sent to me as well as the data is entered into a database. All of this worked properly but what I would like to do now is before the data is sent to the database, I would like to check the database to make sure both the name and email of the user aren't already in the DB. If the name or email is already in the DB then I would liike to send the user to an error page telliing them that either of their entries already exists. Hopefully that makes sense. Here is the code I have so far. The send_contact.php is a script that sends the email.

PHP Code:

<?php include 'send_contact.php' ?>

<?php

$host="localhost"; // Host name 
$username="**********"; // Mysql username 
$password="********"; // Mysql password 
$db_name="barrie_contactinfo"; // Database name 
$tbl_name="contact_info"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$name=$_POST['fullname'];
$email=$_POST['email'];
$hear=$_POST['select2'];

//Check to see if values already exist in the database
$check= mysql_query("SELECT FullName FROM \"$tbl_name\" WHERE FullName=\"$name\"");
$returned = mysql_fetch_array($check);
if(!empty($returned))
    {
    header("Location: error-nameexists.php");
    mysql_close();
    Die();
    }
else {
//Check to see if email exists in the database
$check = mysql_query("SELECT Email from users WHERE Email=\"$email\"");
$returned = mysql_fetch_array($check);
if(!empty($returned))
    {
    header("Location: error-nameexists.php");
    mysql_close();
    Die();
    }




//****Above is the part I am not sure about, basically I'm not sure abot the mysql_query statement.**********  




// Insert data into mysql 
$sql="INSERT INTO $tbl_name(FullName, Email, Hear)VALUES('$name', '$email', '$hear')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message. 
if($result){
echo "Thank you for your interest in the Barrie Men's Hockey League.  We will be in touch with you as soon as possible.<br><br><a href='index.html'>Back to main page</a>";
}

else {
echo "ERROR";
}
}

// close connection 
mysql_close();
?>



Hopefully someone can understand what I saying. Thanks for having a look.


Nick
Last edited by admin : 07-Jun-2007 at 22:33. Reason: Please insert your PHP code between [php] & [/php] tags
  #2  
Old 08-Jun-2007, 12:22
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 730
admin will become famous soon enough

Re: Trying to check the existance of data in MySQL


Hello Nick,

I have edited your post so the code is syntax highlighted, please let me know if I messed it up.

Also, I got side-tracked when preparing my reply to you. So I'll reply in parts.

Some related comments about your PHP script first:

When submitting variables to your functions, you don't need to "quote" them, for example:

PHP Code:

//mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_connect( $host, $username, $password ) or die( "cannot connect" ); 



When using user submitted data like the ones supplied by the GLOBAL variable $_POST and others, you need to be careful with them.
  • Always check if they were set by using isset()
  • removing the slashes that your PHP setup may have inserted to this data.

For example:

PHP Code:

// Get values from form
if( isset($_POST['fullname'])  &&  $_POST['fullname']=trim($_POST['fullname']) )
{
    // remove slashes that PHP may have inserted to this value.
    if( get_magic_quotes_gpc() )
        $_POST['fullname'] = stripslashes( $_POST['fullname'] );
}
//$name=$_POST['fullname']; 


__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #3  
Old 09-Jun-2007, 12:59
ninedoors ninedoors is offline
New Member
 
Join Date: Jun 2007
Posts: 3
ninedoors is on a distinguished road

Re: Trying to check the existance of data in MySQL


Figured it out. Thanks for the help.

Nick
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving data from MySQL to MySQL meurer MySQL / PHP Forum 0 25-Jan-2007 09:03
Cpanel downgrade MYSQL 4.1.X to MySQL 4.0.xx Webhosting-live Web Hosting Forum 1 01-Sep-2006 02:54
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.