homepage Welcome to WebmasterWorld Guest from 54.234.0.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP form to sql issues
Issues with making my php work with my sql
mantobba



 
Msg#: 4530126 posted 8:45 am on Dec 21, 2012 (gmt 0)

I have the following which I am using from a template but adding the address information but it keeps telling me when I send the form that there are is something wrong with my address input and phone number imput.

:
<?php
/*
* Description: Program checks all the form fields for
* blank fields and incorrect format. Saves the
* correct fields in a database.
*/
?>
<html>
<head><title>Member Phone Number</title></head>
<body>
<?php
/* set up array of field labels */
$labels = array( "first_name" => "First Name","last_name" => "Last Name","address" => "Address","phone" => "Phone");
/* Check information from form */
foreach($_POST as $field => $value)
{
/* check each field for blank fields */
if( $value == "" )
{
$blank_array[] = $field;
}
/* check format of each field */
elseif( ereg("(name)",$field) )
{
if(!ereg("^[A-Za-z' -]{1,50}$",$value) )
{
$bad_format[] = $field;
}
}
elseif($field == "address")
{
if ( !ereg("^ [A-Za-z' -] {1-50}$",$value) )
{
$bad_format[] = $field;
}
}
elseif($field == "phone")
{
if ( !ereg("^ [0-9]{1}- [0-9]{3}-[0-9]{3}-[0-9]{4}$",$value) )
{
$bad_format[] = $field;
}
}
} // end of foreach for $_POST
/* if any fields were not okay, display error message and form */
if(@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
{
if(@sizeof($blank_array) > 0)
{
/* display message for missing information */
echo "<b>You didn't fill in one or more required fields.
You must enter:</b><br>";
/* display list of missing information */
foreach($blank_array as $value)
{
echo "&nbsp;&nbsp;&nbsp;{$labels[$value]}<br>";
}
}
if(@sizeof($bad_format) > 0)
{
/* display message for bad information */
echo "<b>One or more fields have information that appears to
be incorrect. Correct the format for:</b><br>";
/* display list of bad information */
foreach($bad_format as $value)
{
echo "&nbsp;&nbsp;&nbsp;{$labels[$value]}<br>";
}
}
/* redisplay form */
echo "<p><hr />";
echo "<h3>Please enter your phone number below.</h3>";
echo "<form action='phone.php' method='POST'>
<table>";
foreach($labels as $field => $label)
{
$good_data[$field]=strip_tags(trim($_POST[$field]));
echo "<tr>
<td style='text-align: right; font-weight: bold'>
$label</td>
<td><input type='text' name='$field' size='65'
maxlength='65' value='$good_data[$field]'></td>
</tr>";
}
echo "<tr>
<td colspan='2' style='text-align: center'>
<input type='submit' value='Submit'>";
echo "</td></tr></table>
</form>";
exit();
}
else //if data is okay
{
$user="********";
$host="localhost";
$password="******";
$database = "ottawaglandorfems_zzl_ogems";
$cxn = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
mysql_select_db($database);
$fields_all = array_keys($labels);
foreach($fields_all as $field)
{
$good_data[$field] = strip_tags(trim($_POST[$field]));
if($field == "phone")
{
$good_data[$field] = ereg_replace("[)( .-]","",$good_data[$field]);
}
$good_data[$field] = mysql_real_escape_string($good_data[$field]);
}

$query = "INSERT INTO phone (Lst_Nm,Frst_Nm,Ph_Num)
VALUES ('$good_data[last_name]','$good_data[first_name]',
'$good_data[phone]')";
$result = mysql_query($query)
or die ("Couldn't execute query.");
echo "<h4>Thank You for Registering</h4>";
}
?>
</body></html>



here is my form information.....



<?php
/*
* Description: Script displays a form that asks for the
* customer phone number.
*/
echo "<html>
<head><title>Customer Info</title></head>
<body>";
$labels = array ( "first_name" => "First Name",
"last_name" => "Last Name", "address" => "Address",
"phone" => "Phone");
echo "<h3>Please enter your phone number below.</h3>";
echo "<form action='phonerep.php' method='POST'>
<table>\n";
/* Loop that displays the form fields */
foreach($labels as $field => $label)
{
echo "<tr>
<td style='text-align: right;
font-weight: bold'> $label</td>
<td><input type='text' name='$field' size='65'
maxlength='65' ></td>
</tr>";
}
echo "<tr>
<td colspan='2' style='text-align: center'>
<input type='submit'
value='Submit'>";
echo "</td></tr></table>
</form>";
?>


I am entering the address such as 123 S Street town state (but I would like to do it as 123 S Street Town,State Zip)

My phone I am doing as XXX XXX XXXX but would like to do it as XXX-XXX-XXXX just not sure how to do it. I am new to php. Thanks for any help given.

 

topr8

WebmasterWorld Senior Member topr8 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4530126 posted 12:12 pm on Dec 28, 2012 (gmt 0)

in your phone expression you have left a gap.

if ( !ereg("^ [0-9]{1}
- [0-9]{3}-[0-9]{3}-[0-9]{4}$",$value) )

... so the expression is expecting a gap

and in your address you have made an error.

if ( !ereg("^ [A-Za-z' -] {1
-50}$",$value) )

... should be a comma

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved