Inserting data from form
<p>I am fairly new to PHP and I am reading thru Learning PHP, MySQL, JavaScript &amp; CSS 2nd edition by Robin Nixon. I am working through a script to insert username and password into a database. The book says to enter code to insert data from form fields. I tried to do that (between the /* <em>/ ) but the query gives me errors.  Also when I omit the query there are no errors. When I put the query in, an error shows at the javascript right after the comments in the /</em>  */.</p>

<pre><code>&lt;?php // adduser.php
require_once 'login.php';


$forename = $surname = $username = $password = $age = $email = "";

if (isset($_POST['forename']))
$forename = fix_string($_POST['forename']);
if (isset($_POST['surname']))
$surname = fix_string($_POST['surname']);
if (isset($_POST['username']))
$username = fix_string($_POST['username']);
if (isset($_POST['password']))
$password = fix_string($_POST['password']);
if (isset($_POST['email']))
$email = fix_string($_POST['email']);

$fail  = validate_forename($forename);
$fail .= validate_surname($surname);
$fail .= validate_username($username);
$fail .= validate_password($password);
$fail .= validate_age($age);
$fail .= validate_email($email);

echo "&lt;html&gt;&lt;head&gt;&lt;title&gt;An Example Form&lt;/title&gt;";

if ($fail == "") {
echo "&lt;/head&gt;&lt;body&gt;Form data successfully validated: $forename,
    $surname, $username, $password, $age, $email.&lt;/body&gt;&lt;/html&gt;";

/*      require_once 'login.php';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password, 
            $db_database) or die('Error connecting to MySQL server.');


$forename = mysqli_real_escape_string(db_server, trim($_POST['forename']));
$surname = mysqli_real_escape_string(db_server, trim($_POST['surname']));
$username = mysqli_real_escape_string(db_server, trim($_POST['username']));
$password = mysqli_real_escape_string(db_server, trim($_POST['password']));
$email = mysqli_real_escape_string(db_server, trim($_POST['email'])); 

$query = "INSERT INTO users VALUES" . "('$forename', '$surname', ". 
        "'$username', '$password', '$email');
$result = mysqli_query($db_server, $query); ". 
    "or die('Error querying database.');

mysqli_close($db_database);   */    


exit;

}



echo &lt;&lt;&lt;_END


&lt;style&gt;.signup { border: 1px solid #999999;
font: normal 14px helvetica; color:#444444; }&lt;/style&gt;
&lt;script type="text/javascript"&gt;
function validate(form)
{
fail  = validateForename(form.forename.value)
fail += validateSurname(form.surname.value)
fail += validateUsername(form.username.value)
fail += validatePassword(form.password.value)
fail += validateEmail(form.email.value)
if (fail == "") return true
else { alert(fail); return false }
}
&lt;/script&gt;&lt;/head&gt;&lt;body&gt;
&lt;table class="signup" border="0" cellpadding="2"
cellspacing="5" bgcolor="#eeeeee"&gt;
&lt;th colspan="2" align="center"&gt;Signup Form&lt;/th&gt;

&lt;tr&gt;&lt;td colspan="2"&gt;Sorry, the following errors were found&lt;br /&gt;
in your form: &lt;p&gt;&lt;font color=red size=1&gt;&lt;i&gt;$fail&lt;/i&gt;&lt;/font&gt;&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;

&lt;form method="post" action="adduser.php"
onSubmit="return validate(this)"&gt;
 &lt;tr&gt;&lt;td&gt;Forename&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="32"
name="forename" value="$forename" /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Surname&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="32"
name="surname" value="$surname" /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Username&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="16"
name="username" value="$username" /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Password&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="12"
name="password" value="$password" /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Age&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="3"
name="age" value="$age" /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Email&lt;/td&gt;&lt;td&gt;&lt;input type="text" maxlength="64"
name="email" value="$email" /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;&lt;td colspan="2" align="center"&gt;
&lt;input type="submit" value="Signup" /&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/form&gt;&lt;/table&gt;



&lt;script type="text/javascript"&gt;
function validateForename(field) {
if (field == "") return "No Forename was entered.\\n"
return ""
}

function validateSurname(field) {
if (field == "") return "No Surname was entered.\\n"
return ""
}

function validateUsername(field) {
if (field == "") return "No Username was entered.\\n"
else if (field.length &lt; 5)
    return "Usernames must be at least 5 characters.\\n"
else if (/[^a-zA-Z0-9_-]/.test(field))
    return "Only letters, numbers, - and _ in usernames.\\n"
return ""
}

function validatePassword(field) {
if (field == "") return "No Password was entered.\\n"
else if (field.length &lt; 6)
    return "Passwords must be at least 6 characters.\\n"
else if (! /[a-z]/.test(field) ||
         ! /[A-Z]/.test(field) ||
         ! /[0-9]/.test(field))
    return "Passwords require one each of a-z, A-Z and 0-9.\\n"
return ""
}

function validateAge(field) {
if (isNaN(field)) return "No Age was entered.\\n"
else if (field &lt; 18 || field &gt; 110)
    return "Age must be between 18 and 110.\\n"
return ""
}

function validateEmail(field) {
if (field == "") return "No Email was entered.\\n"
    else if (!((field.indexOf(".") &gt; 0) &amp;&amp;
               (field.indexOf("@") &gt; 0)) ||
               /[^a-zA-Z0-9.@_-]/.test(field))
    return "The Email address is invalid.\\n"
return ""
}
&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;
_END;


function validate_forename($field) {
if ($field == "") return "No Forename was entered&lt;br /&gt;";
return "";
}

function validate_surname($field) {
if ($field == "") return "No Surname was entered&lt;br /&gt;";
return "";
}

function validate_username($field) {
if ($field == "") return "No Username was entered&lt;br /&gt;";
else if (strlen($field) &lt; 5)
    return "Usernames must be at least 5 characters&lt;br /&gt;";
else if (preg_match("/[^a-zA-Z0-9_-]/", $field))
    return "Only letters, numbers, - and _ in usernames&lt;br /&gt;";
return "";      
}

function validate_password($field) {
if ($field == "") return "No Password was entered&lt;br /&gt;";
else if (strlen($field) &lt; 6)
    return "Passwords must be at least 6 characters&lt;br /&gt;";
else if (!preg_match("/[a-z]/", $field) ||
         !preg_match("/[A-Z]/", $field) ||
         !preg_match("/[0-9]/", $field))
    return "Passwords require 1 each of a-z, A-Z and 0-9&lt;br /&gt;";
return "";
}

function validate_email($field) {
if ($field == "") return "No Email was entered&lt;br /&gt;";
    else if (!((strpos($field, ".") &gt; 0) &amp;&amp;
               (strpos($field, "@") &gt; 0)) ||
                preg_match("/[^a-zA-Z0-9.@_-]/", $field))
    return "The Email address is invalid&lt;br /&gt;";
return "";
}

function fix_string($string) {
if (get_magic_quotes_gpc()) $string = stripslashes($string);
return htmlentities ($string);
}
?&gt;
</code></pre>
