Jump to content


Photo

Simple form to database


  • Please log in to reply
8 replies to this topic

#1 barney0o0

barney0o0
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts

Posted 26 June 2006 - 09:54 AM

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 !!)

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


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

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 June 2006 - 09:58 AM

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).
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 barney0o0

barney0o0
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts

Posted 26 June 2006 - 10:30 AM

[!--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) View Post[/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]

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


#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 June 2006 - 11:08 AM

Change this:
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>

To this:
?>
<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>

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 barney0o0

barney0o0
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts

Posted 26 June 2006 - 12:13 PM

[!--quoteo(post=388025:date=Jun 26 2006, 11:08 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 26 2006, 11:08 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Change this:
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>

To this:
?>
<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>

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)

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

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


Many thanks

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 June 2006 - 12:25 PM

after </form> you need to close your else statement.

</form>
<?php } ?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 barney0o0

barney0o0
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts

Posted 26 June 2006 - 02:09 PM

[!--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) View Post[/div][div class=\'quotemain\'][!--quotec--]
after </form> you need to close your else statement.

</form>
<?php } ?>
[/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

#8 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 26 June 2006 - 03:10 PM

[!--quoteo(post=388067:date=Jun 26 2006, 09:09 AM:name=barney0o0)--][div class=\'quotetop\']QUOTE(barney0o0 @ Jun 26 2006, 09:09 AM) View Post[/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!

<?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;">

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...
"Syntax error" .. WHERE THE **** IS IT?!

#9 barney0o0

barney0o0
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts

Posted 26 June 2006 - 05:25 PM

Cheers Legohead....ill have a go at it tomorrow...many thanks for you offerings




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users