The problem: When I run this script, the only thing that gets displayed in the browser window is the text: "Errors:" from the foreach statement I have. This shouldn't happen if there are no errors in the form fields but somehow is. Also the script is not echoing the user's form information after the query is submitted. Any help would be great, thanks!
The Form:
<form method="post" action="make.php">
<p>
<label for="username">Username:</label>
<input type="text" name="username" id="username" size="10" class="form" />
</p>
<p>
<label for="password1">Password:</label>
<input type="password" name="password1" id="password1" size="10" class="form" />
</p>
<p>
<label for="password2">Confirm Password:</label>
<input type="password" name="password2" id="password2" size="10" class="form" />
</p>
<p>
<label for="email">Email:</label>
<input type="text" name="email" id="email" class="form" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Register" class="submit" />
</p>
</form>
The PHP
<?php
//connect to database
$connect=mysql_connect("localhost","root","");
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
//select database
$db = mysql_select_db("verbazon",$connect) or die("Could not connect: " . mysql_error());
//define variables
$username=$_POST['username'];
$password1=$_POST['password1'];
$password2=$_POST['password2'];
$email=$_POST['email'];
//remove spaces
$username=trim($username);
$password1=trim($password1);
$password2=trim($password2);
$email=trim($email);
//remove html tags
$username=strip_tags($username);
$password1=strip_tags($password1);
$password2=strip_tags($password2);
$email=strip_tags($email);
$errors=array();
if(empty($username))
{
$errors[]= "You must go back and enter a username.";
}
if(empty($password1) || empty($password2) || $password1 != $password2)
{
$errors[]= "You must enter a password twice.";
}
if(empty($email))
{
$errors[]= "You must enter a valid email address.";
}
else
{
eregi("[ a-z||0-9 ] @ [ a-z||0-9 ] . [ a-z ]",$email);
}
if(count($errors > 0))
{
echo "Errors:<br />";
foreach($errors as $err)
{
echo $err . "<br />";
}
}
else
{
//insert values into database
$result=mysql_query("INSERT INTO users(id,username,password1,email)".
"VALUES('NULL', '$username', '$password1', '$email')");
echo "Your name and password have been submitted into our database.";
echo $username . "<br>";
echo $password1 . "<br>";
echo $email . "<br>";
}
}
?>