Jump to content

Database entry


Schlo_50

Recommended Posts

The following code is for an ordering form for a school project. The script is meant to add the values to my database and display a message ('Order Form Complete $firstname!') although this is not the case. Instead the page just re-freshes and up loads the form again. Please Help!

[code]!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Order Form</title>
</head>

<body>
<?php
$self=$_SERVER['PHP_SELF'];
$title=$_POST['title'];
$firstname=$_POST['fname'];
$surname=$_POST['sname'];
$addline1=$_POST['adline1'];
$addline2=$_POST['adline2'];
$city=$_POST['city'];
$postcode=$_POST['postcode'];
$contact=$_POST['hphone'];
$email=$_POST['email'];
?>

<center><form action="link" method="post">
Mr, Mrs, Miss:
    <select name="title" size="1" multiple="multiple">
      <option value="Mr" selected="selected">Mr</option>
      <option value="Mrs">Mrs</option>
      <option value="Miss">Miss</option>
    </select> 
    <br>
First Name: <input type="text" name="fname"><br>
Surname: <input type="text" name="sname"><br>
Address Line 1: <input type="text" name="adline1"><br>
Address Line 2: <input type="text" name="adline2"><br>
City:<input type="text" name="city"><br>
Post Code: <input type="text" name="postcode"><br>
Contact Number: <input type="text" name="hphone"><br>
Email Address: <input type="text" name="email"><br>
<input type="Submit">
</form></center>

<?php
$conn = mysql_connect( "host","database","password" )
or die( "Err:Conn" );
$rs = mysql_select_db( "dombar0_work", $conn )
or die( "Err:Db" );


$sql = "INSERT INTO cust_tbl ( title,fname,sname,adline1,adline2,city,postcode,hphone,email ) VALUES ( \"$title\",\"$firstname\",\"$surname\",\"$addline1\",\"$addline2\",\"$city\",\"$postcode\",
\"$contact\",\"$email\") )";
                           
$rs = mysql_query( $sql, $conn );
if ($rs){ echo( "Order Form Complete $firstname!" ); }
?>
</body>
</html>[/code]

Link to comment
https://forums.phpfreaks.com/topic/29679-database-entry/
Share on other sites

First of all put all the PHP before the HTML, second, try using: mysql_query ("INSERT INTO `table` VALUES ('blah')") and not all that escaping, it's prone to errors. Also, you want to add, if the query is successful:

if ($sql_query)
{
?>yay! success!<?
} else {
?>bleh form<?
}

or something similar
Link to comment
https://forums.phpfreaks.com/topic/29679-database-entry/#findComment-136210
Share on other sites

[code]<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Order Form</title>
</head>

<body>
<?php
$self=$_SERVER['PHP_SELF'];
$title=$_POST['title'];
$firstname=$_POST['fname'];
$surname=$_POST['sname'];
$addline1=$_POST['adline1'];
$addline2=$_POST['adline2'];
$city=$_POST['city'];
$postcode=$_POST['postcode'];
$contact=$_POST['hphone'];
$email=$_POST['email'];
?>
<?php
$conn = mysql_connect( "","dombar0_work","" )
or die( "Err:Conn" );
$rs = mysql_select_db( "dombar0_work", $conn )
or die( "Err:Db" );

if(isset($_POST['submit'])){
$sql = "INSERT INTO 'cust_tbl' VALUES ( \"$title\",\"$firstname\",\"$surname\",\"$addline1\",\"$addline2\",\"$city\",\"$postcode\",\"$contact\",\"$email\") )";}
else {
Poo form
}

                           
$rs = mysql_query( $sql, $conn );
if ($rs){ echo( "Order Form Complete $firstname!" ); }

?>


<center><form action="link" method="post">
Mr, Mrs, Miss:
    <select name="title" size="1" multiple="multiple">
      <option value="Mr" selected="selected">Mr</option>
      <option value="Mrs">Mrs</option>
      <option value="Miss">Miss</option>
    </select> 
    <br>
First Name: <input type="text" name="fname"><br>
Surname: <input type="text" name="sname"><br>
Address Line 1: <input type="text" name="adline1"><br>
Address Line 2: <input type="text" name="adline2"><br>
City:<input type="text" name="city"><br>
Post Code: <input type="text" name="postcode"><br>
Contact Number: <input type="text" name="hphone"><br>
Email Address: <input type="text" name="email"><br>
<input type="Submit">
</form></center>

</body>
</html>[/code]

I screwed up, lol Bear with me please, im new to this game, sorry!
Link to comment
https://forums.phpfreaks.com/topic/29679-database-entry/#findComment-136523
Share on other sites

Here is the revised code
[code]<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Order Form</title>
</head>

<body>
<?php
if(isset($_POST['submit'])){
$title=$_POST['title'];
$firstname=$_POST['fname'];
$surname=$_POST['sname'];
$addline1=$_POST['adline1'];
$addline2=$_POST['adline2'];
$city=$_POST['city'];
$postcode=$_POST['postcode'];
$contact=$_POST['hphone'];
$email=$_POST['email'];

$conn = mysql_connect( "","dombar0_work","" ) or die( "Err:Conn" );
$rs = mysql_select_db( "dombar0_work", $conn ) or die( "Err:Db" );


$sql = "INSERT INTO 'cust_tbl' VALUES ( \"$title\",\"$firstname\",\"$surname\",\"$addline1\",\"$addline2\",\"$city\",\"$postcode\",\"$contact\",\"$email\") )";
$rs = mysql_query( $sql, $conn );
if ($rs){ echo( "Order Form Complete $firstname!" ); }
} else {
$self=$_SERVER['PHP_SELF'];
?>


<center>
<form action="<?=$self?>" method="post">
Mr, Mrs, Miss:
    <select name="title" size="1" multiple="multiple">
      <option value="Mr" selected>Mr</option>
      <option value="Mrs">Mrs</option>
      <option value="Miss">Miss</option>
    </select>
    <br>
First Name: <input type="text" name="fname"><br>
Surname: <input type="text" name="sname"><br>
Address Line 1: <input type="text" name="adline1"><br>
Address Line 2: <input type="text" name="adline2"><br>
City:<input type="text" name="city"><br>
Post Code: <input type="text" name="postcode"><br>
Contact Number: <input type="text" name="hphone"><br>
Email Address: <input type="text" name="email"><br>
<input type="Submit" name=submit value=Submit>
</form>
</center>
<?php
}
?>
</body>
</html>[/code]

couple things to remember with forms, if you are using a check  to see if the form was submitted you have to name the submit button. setting the type to submit works fine to just get the form submitted but will not pass a value for the check. Also the action cannot just say "link" it has to be blank or the name of an actual page to do the work. If it is blank it will use the same page name.

Ray
Link to comment
https://forums.phpfreaks.com/topic/29679-database-entry/#findComment-136862
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.