moverholt31
09-18-2011, 09:29 PM
I'm trying to take the following information from a form and insert it into a database. Assuming that the connection information is correct, I'm not able to get it to insert. I was also trying to get it to keep the values that are correct, if something is found to be incorrect during the error checking process. I hope that this is as clear as mud.
<?php
include('connectdb.php');
$errors = array();
//If request is a form submission
if($_SERVER['REQUEST_METHOD'] =='POST'){
//Validation
//Check that firstname is non-blank
if(0 === preg_match("/\S+/",$_POST['firstname'])){$errors['firstname'] = "Please enter a first name.";}
//Check that lastname is non-blank
if(0 === preg_match("/\S+/",$_POST['lastname'])){$errors['lastname'] = "Please enter a last name.";}
//If no validation errors
if(0 === count($errors)){
//Sanitize all data
$firstname = mysql_real_escape_string($_POST['firstname']);
$middlename = mysql_real_escape_string($_POST['middlename']);
//Connect to the databse
connectdb();
//Insert user into the database
$query = "INSERT INTO members_info
('firstname', 'lastname')
VALUES
('$firstname', '$lastname')";
mysql_query($query);
if(mysql_errno() === 0){
//Registration successful
$user_id = mysql_insert_id();
log_in($user_id);
header("Location: index.php");
}
//Helper functions
function form_row_class($name){
global $errors;
return $errors[$name] ? "form_error_row" : "";
}
function error_for($name){
global $errors;
if($errors[$name]){
return "<div style='color: red;' class='form_error'>" . $errors[$name] . "</div>";
}
}
function h($string){
return htmlspecialchars($string);
}
<html>
<body>
<tr class="<?php echo form_row_class("firstname") ?>">
<td width="25%" align="right" valign="middle">
First Name:
</td>
<td width="193" valign="left">
<input type="text" name="firstname" id="firstname" size="50" maxlength="21" value="<?php h($_POST['firstname']);?>" width="433"/>
<?php echo error_for('firstname') ?>
</td>
</tr>
<tr class="<?php echo form_row_class("lastname") ?>">
<td class="fields" width="25%" align="right" valign="middle">
Last Name:
</td>
<td width="193">
<input type="text" name="lastname" id="lastname" size="50" maxlength="21" value="<?php h($_POST['lastname']);?>" width="433"/>
<?php echo error_for('lastname') ?>
</td>
</tr>
<tr>
<td>
</td>
<td colspan="2" align="left" >
<input type="submit" size="50" name="member_complete_button" value="Create My Account">
</td>
</tr>
</body>
</html>
<?php
include('connectdb.php');
$errors = array();
//If request is a form submission
if($_SERVER['REQUEST_METHOD'] =='POST'){
//Validation
//Check that firstname is non-blank
if(0 === preg_match("/\S+/",$_POST['firstname'])){$errors['firstname'] = "Please enter a first name.";}
//Check that lastname is non-blank
if(0 === preg_match("/\S+/",$_POST['lastname'])){$errors['lastname'] = "Please enter a last name.";}
//If no validation errors
if(0 === count($errors)){
//Sanitize all data
$firstname = mysql_real_escape_string($_POST['firstname']);
$middlename = mysql_real_escape_string($_POST['middlename']);
//Connect to the databse
connectdb();
//Insert user into the database
$query = "INSERT INTO members_info
('firstname', 'lastname')
VALUES
('$firstname', '$lastname')";
mysql_query($query);
if(mysql_errno() === 0){
//Registration successful
$user_id = mysql_insert_id();
log_in($user_id);
header("Location: index.php");
}
//Helper functions
function form_row_class($name){
global $errors;
return $errors[$name] ? "form_error_row" : "";
}
function error_for($name){
global $errors;
if($errors[$name]){
return "<div style='color: red;' class='form_error'>" . $errors[$name] . "</div>";
}
}
function h($string){
return htmlspecialchars($string);
}
<html>
<body>
<tr class="<?php echo form_row_class("firstname") ?>">
<td width="25%" align="right" valign="middle">
First Name:
</td>
<td width="193" valign="left">
<input type="text" name="firstname" id="firstname" size="50" maxlength="21" value="<?php h($_POST['firstname']);?>" width="433"/>
<?php echo error_for('firstname') ?>
</td>
</tr>
<tr class="<?php echo form_row_class("lastname") ?>">
<td class="fields" width="25%" align="right" valign="middle">
Last Name:
</td>
<td width="193">
<input type="text" name="lastname" id="lastname" size="50" maxlength="21" value="<?php h($_POST['lastname']);?>" width="433"/>
<?php echo error_for('lastname') ?>
</td>
</tr>
<tr>
<td>
</td>
<td colspan="2" align="left" >
<input type="submit" size="50" name="member_complete_button" value="Create My Account">
</td>
</tr>
</body>
</html>