Jump to content

Simple form to database


barney0o0

Recommended Posts

Hi, i hope someone could help me with this...im trying to write a simple form that will update a mysql record. The actual form has vaildation and error messages showing ontop of the form....what i have at the moment just doesnt work (sorry im a newb to php !!)

[code]<?php require_once('file:///D|/wamp/www/collage/Connections/Collage.php'); ?>
<?php $email_addr = $_REQUEST['email']

$errors = false;

if($_REQUEST['firstname']==''){
echo "You must enter your first name";
$errors = true;
}
if($_REQUEST['lastname']==''){
echo "You must enter your last name";
$errors = true;
}
if(!preg_match("/[a-zA-Z0-9]*\@(\.[a-zA-Z0-9])*/"),$email_addr){
echo "You have entered an invalid email address. Please verify it is correct before continuing";
$errors = true;
}

if($errors==false){

$sql = "INSERT INTO per2
(firstname2,lastname2,age2)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
##Run the query..
mysql_query($sql,$con);
}
else{
echo "Sorry I wasn't able to add your record because there are errors in your field. Please make sure the data was entered correctly and resubmit the form!";
echo "?>
<form action=\"inserttest.php\" method=\"POST\">
Enter your Firstname: <input type=\"text\" name=\"firstname\" value ='{$_REQUEST['firstname']}' />
Enter your Lastname: <input type=\"text\" name=\"lastname\" value ='{$_REQUEST['lastname']}' />
Enter your Age: <input type=\"text\" name=\"age\" value ='{$_REQUEST['age']}' />
<input type=\"submit\" />
</form>[/code]


Im getting errors straight away (on line 4!)....i know that this needs a bit of a re write.....if theres someone king enought to hold my hand i would appreciate it

Many thanks in advance
Link to comment
Share on other sites

your first error is that you forgot a ; at the end of line 4

<?php $email_addr = $_REQUEST['email'][!--coloro:red--][span style=\"color:red\"][!--/coloro--];[!--colorc--][/span][!--/colorc--]

p.s. - if you read your error it will usually most oftenly give you a clue as to what you did wrong. but if you have no idea what it means, if you post the error message, you will get helped faster (if at all).
Link to comment
Share on other sites

[!--quoteo(post=387992:date=Jun 26 2006, 09:58 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 26 2006, 09:58 AM) [snapback]387992[/snapback][/div][div class=\'quotemain\'][!--quotec--]
your first error is that you forgot a ; at the end of line 4

<?php $email_addr = $_REQUEST['email'][!--coloro:red--][span style=\"color:red\"][!--/coloro--];[!--colorc--][/span][!--/colorc--]

p.s. - if you read your error it will usually most oftenly give you a clue as to what you did wrong. but if you have no idea what it means, if you post the error message, you will get helped faster (if at all).
[/quote]


Thankyou...ive been looking at the script for the last 4 hours and didnt see this missing semi colon....anyways the upto date script is below, now i getting the error message (line 36 being the last line '</form> '

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Parse error: parse error, unexpected $end in D:\wamp\www\collage\update\inserttest.php on line 36[/quote]

[code]<?php require_once('../Connections/Collage.php'); ?>
<?php $email_addr = $_REQUEST['email'];

$errors = false;

if($_REQUEST['firstname']==''){
echo "You must enter your first name";
$errors = true;
}
if($_REQUEST['lastname']==''){
echo "You must enter your last name";
$errors = true;
}
if(!preg_match("/[a-zA-Z0-9]*\@(\.[a-zA-Z0-9])*/",$email_addr)){
echo "You have entered an invalid email address. Please verify it is correct before continuing";
$errors = true;
}

if($errors==false){

$sql = "INSERT INTO per2
(firstname2,lastname2,age2)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
##Run the query..
mysql_query($sql,$con);
}
else{
echo "Sorry I wasn't able to add your record because there are errors in your field. Please make sure the data was entered correctly and resubmit the form!";
echo "?>
<form action=\"inserttest.php\" method=\"POST\">
Enter your Firstname: <input type=\"text\" name=\"firstname\" value ='{$_REQUEST['firstname']}' />
Enter your Lastname: <input type=\"text\" name=\"lastname\" value ='{$_REQUEST['lastname']}' />
Enter your Age: <input type=\"text\" name=\"age\" value ='{$_REQUEST['email']}' />
<input type=\"submit\" />
</form> [/code]
Link to comment
Share on other sites

Change this:
[code]echo "?>
<form action=\"inserttest.php\" method=\"POST\">
Enter your Firstname: <input type=\"text\" name=\"firstname\" value ='{$_REQUEST['firstname']}' />
Enter your Lastname: <input type=\"text\" name=\"lastname\" value ='{$_REQUEST['lastname']}' />
Enter your Age: <input type=\"text\" name=\"age\" value ='{$_REQUEST['email']}' />
<input type=\"submit\" />
</form>[/code]

To this:
[code]?>
<form action="inserttest.php" method="POST">
Enter your Firstname: <input type="text" name="firstname" value ="<?php echo $_REQUEST['firstname']; ?>" />
Enter your Lastname: <input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
Enter your Age: <input type="text" name="age" value="<?php echo $_REQUEST['email']; ?>" />
<input type="submit" />
</form>[/code]

Orio.
Link to comment
Share on other sites

[!--quoteo(post=388025:date=Jun 26 2006, 11:08 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 26 2006, 11:08 AM) [snapback]388025[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Change this:
[code]echo "?>
<form action=\"inserttest.php\" method=\"POST\">
Enter your Firstname: <input type=\"text\" name=\"firstname\" value ='{$_REQUEST['firstname']}' />
Enter your Lastname: <input type=\"text\" name=\"lastname\" value ='{$_REQUEST['lastname']}' />
Enter your Age: <input type=\"text\" name=\"age\" value ='{$_REQUEST['email']}' />
<input type=\"submit\" />
</form>[/code]

To this:
[code]?>
<form action="inserttest.php" method="POST">
Enter your Firstname: <input type="text" name="firstname" value ="<?php echo $_REQUEST['firstname']; ?>" />
Enter your Lastname: <input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
Enter your Age: <input type="text" name="age" value="<?php echo $_REQUEST['email']; ?>" />
<input type="submit" />
</form>[/code]

Orio.
[/quote]

Thanks for you changes...however still problems!:)...
Ive revisited the script and realised a few problems, i.e not specify the connection (der)

I now get

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Parse error: parse error, unexpected $end in D:\wamp\www\collage\update\inserttest.php on line 49[/quote]

Line 49 is the last line in the whole script (without any code)

[code]<?php

$email_addr = $_REQUEST['email'];

$errors = false;

if($_REQUEST['firstname']==''){
echo "You must enter your first name";
$errors = true;
}
if($_REQUEST['lastname']==''){
echo "You must enter your last name";
$errors = true;
}
if(!preg_match("/[a-zA-Z0-9]*\@(\.[a-zA-Z0-9])*/",$email_addr)){
echo "You have entered an invalid email address. Please verify it is correct before continuing";
$errors = true;
}

if($errors==false){

$con = mysql_connect('**', '**', '**');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("", $con);

$sql="INSERT INTO per2
(firstname2,lastname2,age2)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());

}
else{
echo "Sorry I wasn't able to add your record because there are errors in your field. Please make sure the data was entered correctly and resubmit the form!";
?>
<form action="inserttest.php" method="POST">
Enter your Firstname: <input type="text" name="firstname" value ="<?php echo $_REQUEST['firstname']; ?>" />
Enter your Lastname: <input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
Enter your Age: <input name="email" type="text" id="email" value="<?php echo $_REQUEST['email']; ?>" />
<input type="submit" name="Submit" value="Submit">
</form>[/code]

I hope someone can help me...im going up the wall with this one!


Many thanks
Link to comment
Share on other sites

[!--quoteo(post=388040:date=Jun 26 2006, 12:25 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 26 2006, 12:25 PM) [snapback]388040[/snapback][/div][div class=\'quotemain\'][!--quotec--]
after </form> you need to close your else statement.

[code]
</form>
<?php } ?>
[/code]
[/quote]


aaah...i still cant get it to work....i thought that this would be rather simple but im starting to tear my hair out...

Could someone point me to a simple complete script that i could use and learn from? (Ideally with error messages at the top of the form (that i was trying to achieve!)

Many thanks for your support with this one

b
Link to comment
Share on other sites

[!--quoteo(post=388067:date=Jun 26 2006, 09:09 AM:name=barney0o0)--][div class=\'quotetop\']QUOTE(barney0o0 @ Jun 26 2006, 09:09 AM) [snapback]388067[/snapback][/div][div class=\'quotemain\'][!--quotec--]
aaah...i still cant get it to work....i thought that this would be rather simple but im starting to tear my hair out...

Could someone point me to a simple complete script that i could use and learn from? (Ideally with error messages at the top of the form (that i was trying to achieve!)

Many thanks for your support with this one

b
[/quote]


HERES A REGISTRATION SCRIPT I USE FOR MY ONLINE MARKETPLACE! THERE ARE TON S OF FIELDS THOUGH!

[code]<?php
if(isset($_POST['submit']))
{
$errors = array();

$agree=$_POST['useragree'];
if($agree != 1){
echo "You Need To agree With the User Agreement to Register!";
}else{
if(empty($_POST['fi'])){
        $errors[] = '<font color=\"yellow\">*Please enter your first Name!</font>';
}else{
$first=$_POST['fi'];
}
if(empty($_POST['la'])){
        $errors[] = '</p>
<p><font color=\"yellow\">*Please enter your last Name!</font></p>';
        }else{
        $last=$_POST['la'];
        }
        if(empty($_POST['em'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter your Email!</font></p>';
        }else{
        $em=$_POST['em'];
        }
        if(empty($_POST['us'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter a username!</font></p>';
        }else{
        $us=$_POST['us'];
        }
        if(empty($_POST['pa'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter a password!</font></p>';
        }else{
        $pa=$_POST['pa'];
        }
        if(empty($_POST['co'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter a Country!</font></p>';
        }else{
        $co=$_POST['co'];
        }
        if(empty($_POST['sp'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter your state or Province!</font></p>';
        }else{
        $sp=$_POST['sp'];
        }
        if(empty($_POST['zp'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter your zip or Postal code!</font></p>';
        }else{
        $zp=$_POST['zp'];
        }
        if(empty($_POST['st'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter your Street Address!</font></p>';
        }else{
        $st=$_POST['st'];
        }
        if(empty($_POST['cty'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter a city!</font></p>';
        }else{
        $cty=$_POST['cty'];
        }
        if(empty($_POST['phone'])){
        $errors[] = '<p><font color=\"yellow\">*Please enter a Phone Number</font></p>';
        }else{
        $phone=$_POST['phone'];
        }
if (empty($errors)) {
$host = "********";
$user = "********";
$pass = "*********";
$db = "***********";



$connection = mysql_connect($host,$user,$pass) or die ("Unable to
connect!");

mysql_select_db($db) or die ("Unable to select database!");

$query = "INSERT INTO members VALUES ('','$fi','$la','$em','$us','$pa','$st','$zp','$co','$sp','$cty','$phone')";
mysql_query($query);
echo "Thankyou, You are now a member";
}else{
foreach ($errors as $key){
echo "$key";
}
}
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="store.css">
</head>
<body>
<p>

<form method="POST">
    <p>First Name:<input type="text" name="fi" size="20"></p>
    <p>Last Name:<input type="text" name="la" size="20"></p>
    <p>Email:<input type="text" name="em" size="37"></p>
    <p>Desired Username:<input type="text" name="us" size="26"></p>
    <p>Desired Password<input type="password" name="pa" size="23"></p>
    <p>Country:<input type="text" name="co" size="24"></p>
    <p>State/Province<input type="text" name="sp" size="27"></p>
    <p>Zip/Postal Code<input type="text" name="zp" size="20"></p>
    <p>City<input type="text" name="cty" size="20"></p>
    <p>Phone#<input type="text" name="phone" size="20"></p>
    <p>Street Address:<br>
    <textarea rows="3" name="st" cols="24"></textarea></p>
    <input type=checkbox name=useragree value=1>Yes, I agree to follow by the <a href=Tos.php target="_blank">User Agreement!</a>
    <p><input type="submit" value="Submit" name="submit"></p>
</form>
<br>
<br>
<INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;">[/code]

there is alot of stuff in there you dont need (I.E the go back button and the agreement thengy) when you remove those this code is pretty back for error checking etc...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.