LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2007, 09:06 PM   #1
jojojo
Member
 
Registered: Feb 2005
Location: Kuala Lumpur, Malaysia
Distribution: Fedora Core 5
Posts: 121

Rep: Reputation: 15
Form registration problem in PHP


Hello everybody. Im a newbie in php and I really need your guys help and opinion on my problem. I have created a simple registration form in php and Im having problem of detecting existing user in database. For example, if I enter staff id number that already existed, I want it to detect and display an error message stating the person already existed. Below are my code:

Code:
<?php

include ('mysql_connect.php');

if (isset($_POST["Submit"]))
{
	if (empty($_POST['staffid']))
	{
		echo "Please enter the staffid";
	}
	else
	$staffid = $_POST['staffid'];
	
	if (empty($_POST['name']))
	{
		echo "Please enter the name";
	}
	else
	$name = $_POST['name'];
	
	if (empty($_POST['address']))
	{
		echo "Please enter staff address";
	}
	else
	$address = $_POST['address'];

	if (empty($_POST['department']))
	{
		echo "Please enter the department";
	}
	else
	$department = $_POST['department'];
	
	
	if ($staffid && $name && $address && $department)
	{
	
		$query1 = "SELECT * FROM staff";
		$result1 = @mysql_query($query1);
		$row = mysql_fetch_array($result1);
			
		if ($row['Staff_ID'] != $staffid)
		{
		$query = "INSERT INTO staff VALUES ('$name', '$address',        '$staffid', '$department', now())";
		$result = @mysql_query($query);
			
		if ($result)
		{
		echo "$name information has been saved in database";
		}
				
				
		}
		else 
		{
		echo "The person of staff id $staffid has already existed";
		}	
	}
}
?>


<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <table width="75%" border="0">
    <tr> 
      <td><font face="Arial, Helvetica, sans-serif">Staff ID</font></td>
      <td><input type="text" name="staffid"></td>
    </tr>
    <tr> 
      <td><font face="Arial, Helvetica, sans-serif">Name</font></td>
      <td><input type="text" name="name"></td>
    </tr>
    <tr> 
      <td><font face="Arial, Helvetica, sans-serif">Address</font></td>
      <td><textarea name="address"></textarea></td>
    </tr>
    <tr> 
      <td><font face="Arial, Helvetica, sans-serif">Department</font></td>
      <td><input type="text" name="department"></td>
    </tr>
  </table>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
  </form>

Note: The column in the database are:

Name, Address, Staff_ID (primary key), Department, Date_reg
 
Old 08-30-2007, 01:08 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Fist check to see if the staff id exists on the database. You can do that by using the WHERE clause
PHP Code:
$query1 SELECT Staff_ID FROM Staff WHERE Staff_ID $_POST['staffid']; 
The row count should be 1 if a record exists 0 otherwise.
There are other issues as well with this code (directly accessing the $_POST for one) but that should get you going.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Self registration form for Mail server manya Linux - Security 1 01-18-2007 07:25 PM
Developing auto registration Form for Mail server manya Linux - General 1 01-18-2007 06:13 PM
NetReg and Online User Registration form caps_phisto Linux - General 2 09-25-2006 07:34 AM
qmail web account registration form yyahmee Linux - Software 2 01-20-2006 07:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration