Hi All
This is my first post so please go easy...
I've built a small site for my fiance's pr agency. It's nothing fancy at all, but at present when they get a new job I simply use phpMyAdmin to insert the various records required by the scripts I wrote to display the jobs.
That's becoming a bit of a bind so I wanted to create a secure area for her to be able to insert the relevant data herself. I read up on sessions/cookies etc. and this morning started on a serious of scripts that would allow me create users who would be able to access this secure area.
The scripts concerned are:
'newuserinsert.php' which is basicaly just a form for collecting the data.
'newuserprocess.php' which does the data validation and either re-directs back to 'newuserinsert.php' (if the data is incorrect) or inserts the data into the database.
'newuserconfirm.php'. not built yet but will confirm to the user their account has been created.
It was going fine until about an hour ago when I hit the following problem:
if the 'tbl_users' table in my database contains a row with null values in all fields (except the unique identifier) then the scripts on these pages work as required.
HOWEVER... if I delete the "null row" from the database and re-submit the form the error checking in page 2 seems to get by-passed entirely and a new 'null row' is created in the table.
I've posted below the scripts, and I hope that makes sense, but if not please let me know if you need clarification on anything.
Thanks in advance
Shane
P.S. please ignore the lack of password encryption as that is the next thing on the list.
NEWUSERINSERT.PHP
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META HTTP-EQUIV="Description"
CONTENT="blah">
<META HTTP-EQUIV="Keywords"
CONTENT="blah">
<TITLE>blah</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<LINK REL=StyleSheet HREF="blah/t_unique.css" TYPE="text/css">
</head>
<BODY>
<div class="main">
<?php
//print header
require('../includes/t_header_include.php');
?>
<div class="h01">Create New User</div>
<div class="details">
<?php
echo session_id();
if (isset($SESSION["errorString"])&& $SESSION["errorString"] != "")
{
echo $_SESSION["errorString"];
}
else
{
echo "<p>Please enter the details requested below to create a new user account.</p>";
}
?>
<form action='t_newuserprocess.php' method='POST'>
<fieldset>
<p><label for="formname">Enter your First Name</label> <input type='text' name='name' id='formname'/></p>
<p><label for="formuser">Choose a Username</label> <input type='text' name='user' id='formuser'/></p>
<p><label for="formpass">Choose a Password</label> <input type='password' name='pass' id='formpass'/></p>
<p class="submit"><input type='submit' value='Create User'/></p>
</fieldset>
</form>
<?php
?>
</div>
<?php
require('../includes/t_footer_include.php');
?>
</div>
</BODY>
</HTML>
NEWUSERPROCESS.PHP
<?php
session_start();
// includes
include '../includes/error.php';
include '../includes/db.php';
// clean session variables
$_SESSION["errorString"] = '';
// clean and trim the POSTed values
$HTTP_POST_VARS['name'] = trim(clean($HTTP_POST_VARS['name'], 30));
$HTTP_POST_VARS['user'] = trim(clean($HTTP_POST_VARS['user'], 30));
$HTTP_POST_VARS['pass'] = trim(clean($HTTP_POST_VARS['pass'], 32));
// validate the POST values
if (empty($HTTP_POST_VARS['name']))
{
$SESSION['errorString'] .="\n<BR>You must enter your first name.";
}
if (empty($HTTP_POST_VARS['user']))
{
$SESSION['errorString'] .="\n<BR>You must choose a username.";
}
if (empty($HTTP_POST_VARS['pass']))
{
$_SESSION['errorString'] .="\n<BR>You must choose a password.";
}
if (!($connection = @ mysql_connect('localhost', $username, $password)))
die("Could not connect to the database");
if (!mysql_select_db($database, $connection))
showerror();
$d = $HTTP_POST_VARS['user'];
$query = "select userID from tbl_users where userName='$d'";
$result=mysql_query($query);
$num=mysql_numrows($result);
//close db connection
mysql_close();
if ($num != "0")
{
$_SESSION['errorString'] = "\n<BR>The username '$d' is already in use. Please choose another username.";
}
// check if there were any errors
if (!empty($_SESSION['errorString']))
{
// there are errors. Re-direct the user to the create user page and advise
// them of the errors.
$HTTP_POST_VARS['name'] = "";
$HTTP_POST_VARS['user'] = "";
$HTTP_POST_VARS['pass'] = "";
header("Location: t_newuserinsert.php");
}
// if we made it here then the data is valid
if (!($connection = @ mysql_connect('localhost', $username, $password)))
die("Could not connect to the database");
if (!mysql_select_db($database, $connection))
showerror();
// declare variables
$a = $HTTP_POST_VARS['user'];
$b = $HTTP_POST_VARS['pass'];
$c = $HTTP_POST_VARS['name'];
// create insert query
$query = "INSERT INTO tbl_users
set userID = NULL, " .
"userName = '$a', " .
"userPass = '$b', " .
"firstName = '$c'";
// run the query
if (!(@ ($query, $connection)))
showerror();
// find the new userID
$userID = mysql_insert_id();
//close db connection
mysql_close();
// redirect to the new user welcome page
$HTTP_POST_VARS['name'] = "";
$HTTP_POST_VARS['user'] = "";
$HTTP_POST_VARS['pass'] = "";
$_SESSION["errorString"] = '';
header("location: t_newuserconfirm.php?userID=$userID");
?>
TBL_USERS
This simply contains 4 fields - userID, userName, userPass and firstName.