...

View Full Version : Inserting Data into Database



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>

myfayt
09-18-2011, 10:03 PM
Please use the PHP code tags when posting code, it highlights the code, and color codes it, so it's easier to find problems.

Also why is this showing middlename instead of lastname?


$firstname = mysql_real_escape_string($_POST['firstname']);
$middlename = mysql_real_escape_string($_POST['middlename']);

Wanna
09-19-2011, 10:45 AM
mysql_real_escape_string needs a valid mysql connection.

Try to put the function connectdb before the mysql_real_escape_string

and also, Use: mysql_query($query) or die(mysql_error());



<?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)){

//Connect to the databse // Replaced
connectdb(); // Replaced

//Sanitize all data
$firstname = mysql_real_escape_string($_POST['firstname']);
$middlename = mysql_real_escape_string($_POST['middlename']);
// NOTE: In the preg_match and the form you use lastname and here middlename. Where did you get the middle name from?
//$lastname = mysql_real_escape_string($_POST['lastname']; // Added

//Insert user into the database
$query = "INSERT INTO members_info
('firstname', 'lastname')
VALUES
('$firstname', '$lastname')";

mysql_query($query) or die(mysql_error()); // Editted

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);
}
// Close PHP
?>
<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>

Inigoesdr
09-19-2011, 05:41 PM
Please remember to read the stickies for this forum. In particular the one about using
tags (http://www.codingforums.com/showthread.php?t=68462) when posting code. You'll notice you are missing a closing PHP tag due to the coloring of the text when you use the proper tags.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum