look at the code .. 😕 why can't i get it to add new users into my MySQL database ?
<center><font color=red><body bgcolor="black">
<?php
function flag_error ($fieldname, $errstr)
{
global $fields_with_errors, $errors;
$fields_with_errors[$fieldname] = 1;
$errors[] = $errstr;
}
function has_error ($fieldname)
{
global $fields_with_errors;
if (isset ($fields_with_errors[$fieldname]))
return true;
return false;
}
define (DB_ERR_DOC, '<html><body>Due to an internal error on the server ' .
'your request cannot be processed at this time. ' .
'Please try again in a few minutes.</body></html>');
$p =& $_POST;
$errors = array();
if (count ($p) > 0)
{
// error checking
if (trim ($p['firstname']) == '')
flag_error ('firstname', 'Please enter your first name.');
if (trim ($p['lastname']) == '')
flag_error ('lastname', 'Please enter your last name.');
if (trim ($p['username']) == '')
flag_error ('username', 'Please enter a username.');
elseif (eregi ('[^a-z0-9]', $p['username']))
flag_error ('username', 'The username you chose is not valid. ' .
'Usernames may only contain letters and digits.');
if (trim ($p['password']) == '')
flag_error ('password', 'Please enter a password.');
// if data is otherwise valid, try to insert record
if (count ($errors) == 0)
{
$dbh = connect ("host=localhost dbname=Gangwars");
if (!$dbh) die (DB_ERR_DOC);
// wrap everything in a transaction
$sql = 'BEGIN TRANSACTION';
$res = exec ($dbh, $sql);
if (!$res) die (DB_ERR_DOC);
freeresult ($res);
// check whether username is unique
$sql = sprintf ("SELECT count (*) FROM Accounts WHERE username = '%s'",
addslashes ($p['username']));
$res = exec ($dbh, $sql);
if (!$res) die (DB_ERR_DOC);
$count = (int) result ($res, 0, 0);
if ($count == 0) $is_unique = TRUE;
else $is_unique = FALSE;
freeresult ($res);
if ($is_unique == FALSE)
{
flag_error ('username', 'The username you chose is already ' .
'in use. Please pick another username.');
$sql = 'ABORT TRANSACTION';
$res = exec ($dbh, $sql);
freeresult ($res);
}
else
{
// does the user accept cookies?
$eats_cookies = isset ($p['setcookie']) ? 'y' : 'n';
// get the next value from the Accountid sequence
$sql = "SELECT nextval('Accounts_Accountid_seq')";
$res = exec ($dbh, $sql);
if (!$res) die (DB_ERR_DOC);
$Accountid = result ($res, 0, 0);
freeresult ($res);
// insert the user's record into the table
$sql = sprintf ("INSERT INTO Gangwars (Accountid, username, firstname,
lastname, password)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",
$Accountid, $p['username'],
addslashes ($p['firstname']),
addslashes ($p['lastname']),
addslashes ($p['password']),
$eats_cookies);
$res = exec ($dbh, $sql);
if (!$res) die (DB_ERR_DOC);
freeresult ($res);
$sql = 'COMMIT TRANSACTION';
$res = exec ($dbh, $sql);
freeresult ($res);
}
close ($dbh);
}
if (count ($errors) == 0)
{
// SUCCESS!
$msg = sprintf ('User account created for <b>%s %s</b> with ' .
'username <b>%s</b> and AccountID <b>%s</b>.',
$p['firstname'], $p['lastname'],
$p['username'], $Accountid);
print ('<html><body>' . $msg . '</body></html>');
exit;
}
}
?>
<html>
<head>
<title>Sign Up Today!</title>
<style type="text/css">
* { font-family: sans-serif }
*.error { font-size: smaller;
font-weight: bold;
color: red; }
</style>
</head>
<h2><img src="http://khashul.tripod.com/Sign_Up_Title"></h2>
<hr noshade>
<small>This Is The Sign Up Page For Gangwars Please Enter Your Details Below</small>
<?php
if (count ($errors) > 0)
{
?>
<p class="error">There were errors in your form. Please correct
the following errors and resubmit the form:</p>
<ul>
<?php
$n = count ($errors);
for ($i = 0; $i < $n; $i++)
print ("<li class=\"error\">" . $errors[$i] . "</li>\n");
?>
</ul>
<?php
}
?>
<form action="<?php print ($PHP_SELF); ?>" method="POST">
<table border="0">
<tr>
<td align="right">
<font size="1"><font color=red><b>FIRST NAME</b></font></td>
<td align="left">
<input type="text" name="firstname"
value="<?php if (isset ($p['firstname'])) print ($p['firstname']);
?>" />
<?php if (has_error ('firstname')) print ('<span class="error">
*</span>'); ?></td>
</tr>
<tr>
<td align="right">
<font size="1"><font color=red><b>LAST NAME</b></font></td>
<td align="left">
<input type="text" name="lastname"
value="<?php if (isset ($p['lastname'])) print ($p['lastname']);
?>" />
<?php if (has_error ('lastname')) print ('<span class="error">
*</span>'); ?></td>
</tr>
<tr>
<td align="right">
<font size="1"><font color=red><b>USERNAME</b></font></td>
<td align="left">
<input type="text" name="username"
value="<?php if (isset ($p['username'])) print ($p['username']);
?>" />
<?php if (has_error ('username')) print ('<span class="error">
*</span>'); ?></td>
</tr>
<tr>
<td align="right">
<font size="1"><font color=red><b>PASSWORD</b></font></td>
<td align="left">
<input type="password" name="password"
value="<?php if (isset ($p['password'])) print ($p['password']);
?>" />
<?php if (has_error ('password')) print ('<span class="error">
*</span>'); ?></td>
</tr>
<tr>
<td align="center" colspan="2">
<font size="1"><font color=red><b>REMEMBER PASSWORD:</b></font>
<input type="checkbox" name="setcookie" value="1"
<?php if (isset ($p['setcookie'])) print (' checked'); ?> /></font></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>