Jump to content

PHP form not submitting


Schlo_50

Recommended Posts

Hello, my form here won't send anything to my database. I hit submit and the page reloads with the message 'Form not submitted Correctly' although im positively unsure why?? lol
Please help!

Here is the code:
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0>
<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>
<center>
<form action="http://dombar0.freehostia.com/forms.php" 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
$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'];

$conn = mysql_connect( "host","username","password" ) or die( "Error Connecting to Db" );
$rs = mysql_select_db( "dombar0_work", $conn ) or die( "Error selecting Db" );


$sql = "INSERT INTO 'cust_tbl' ( title,fname,sname,adline1,adline2,city,postcode,hphone,email ) VALUES ( \"$title\",\"$firstname\",\"$surname\",\"$addline\",\"$addline\",\"$city\",\"$postcode\",\"$contact\",\"$email\") )";
$rs = mysql_query( $sql, $conn );
if ($rs){ echo( "Order Form Complete $firstname!" ); }
else { echo( "Form not submitted correctly!" ); }
?>

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

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/30214-php-form-not-submitting/
Share on other sites

[quote author=ProjectFear link=topic=118165.msg482578#msg482578 date=1165838064]
i dont no why your using \" in front of your variables. just use the plain ' and remove the \. you can add curly brackets around your variable {$var} like so. but otherwise check each variable...
[/quote]
i agree with ProjectFear

and also why $rs = mysql_query( $sql, $conn );

try without the $conn like:
$rs = mysql_query( $sql);


you already made the connection and selected the db

I still can't get anything to load to the database!  ??? Something is still wrong with it..

[quote]<?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'];

$conn = mysql_connect( "mysql2.freehostia.com","dombar0_work","password" ) or die( "Error Connecting to Db" );
$rs = mysql_select_db( "dombar0_work", $conn ) or die( "Error selecting Db" );


$sql = "INSERT INTO 'cust_tbl' VALUES ( '$title' '$firstname' '$surname' '$addline' '$addline' '$city' '$postcode' '$contact' '$email') )";
$rs = mysql_query( $sql);
if ($rs){ echo( "Order Form Complete $firstname!" ); }
else { echo( "Form not submitted correctly!" ); }
?>
[/quote]

Also, if you have a field in your database that is auto incrementing do you have to include that in your script in the 'INSERT INTO' bit?
[quote]Also, if you have a field in your database that is auto incrementing do you have to include that in your script in the 'INSERT INTO' bit?[/quote]

You do have to account for it but not in the insert into. Once you've got the sql worked out (as craygo said add the debugging tool), then try adding this :

[code]
$sql1="select max(cid) from 'cust_tbl'  ";

$result1 = mysql_query($sql1) or die (mysql_error());
$myrow = mysql_fetch_row($result1);
$cid = $myrow[0];[/code]

You might also consider changing your form action to a simpler

[code]<form method="post" action="forms.php">[/code]
In your query line:

[code]<?php
$sql = "INSERT INTO 'cust_tbl' VALUES ( '$title' '$firstname' '$surname' '$addline' '$addline' '$city' '$postcode' '$contact' '$email') )";
?>[/code]

You need to separate those values with commas. Like this:

[code]<?php
$sql = "INSERT INTO 'cust_tbl' VALUES ( '$title', '$firstname', '$surname', '$addline', '$addline', '$city', '$postcode', '$contact', '$email') )";
?>[/code]

Also, in this line:

[code]<?php
$rs = mysql_select_db( "dombar0_work", $conn ) or die( "Error selecting Db" );
?>[/code]

It says your database name is the same as your password ( "dombar0_work" for both ). Is that correct? If either is wrong then it won't work.
Well thats what i have so far, but no can do..lol Unless you want to reise it for me! ha ha


[quote]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0>
<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>
<center>
<form action="http://dombar0.freehostia.com/forms.php" 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
$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'];

$conn = mysql_connect( "mysql2.freehostia.com","dombar0_work","password" ) or die( "Error Connecting to Db" );
$rs = mysql_select_db( "dombar0_work", $conn ) or die( "Error selecting Db" );


$sql = "INSERT INTO 'cust_tbl' VALUES ( '$title', '$firstname', '$surname', '$addline', '$addline', '$city', '$postcode', '$hphone', '$email') )";
   $rs = mysql_query( $sql);
   if ($rs){ echo( "Order Form Complete $firstname!" ); }
else { echo( "Form not submitted correctly!" ); }
?> [/quote]


</body>
</html>
I modified the post ages ago..
This is my now revised code, but now get the error message:

'Parse error: parse error, unexpected T_VARIABLE in /home/www/dombar0.freehostia.com/form2.php on line 43'

I thought this was because i have missed a ; or  space somewhere but can't see it..


[quote]
<html><head><title>Order Form</title></head><body>

<form action="http://dombar0.freehostia.com/form2.php" 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="firstname"><br>
Surname: <input type="text" name="surname"><br>
Address Line 1: <input type="text" name="addline1"><br>
Address Line 2: <input type="text" name="addline2"><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>


<?php
$self = $_SERVER['PHP_SELF'];
$title = $_POST['title'];
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$addline1 = $_POST['addline1'];
$addline2 = $_POST['addline2'];
$city = $_POST['city'];
$postcode = $_POST['postcode'];
$hphone = $_POST['hphone'];
$email = $_POST['email'];


{ $conn = @mysql_connect( "mysql2.freehostia.com", "dombar0_work", "password" )
or die("Could not connect to MySQL");

  $db = @mysql_select_db( "dombar0_work", $conn )
or die("Could not select a database");

  $sql = "INSERT INTO 'cust_tbl' (title,fname,sname,adline1,adline2,city,postcode,hphone,email) VALUES
  ( "$title", "$firstname", "$surname", "$addline1", "$addline2", "$city", "$postcode", "$hphone", "$email" )";

  $result = @mysql_query( $sql )
or die("Could not execute the query");

if( $result ) { echo( "Order form complete $firstname!" ); }
}
?>
</body></html>[/quote]

Thanks for all your help and patience guys! ;)
wrap it in [code=php:0][/code] or [code][/code]. but why do you have so much spaces between your variables.
[code=php:0]
$self =      $_SERVER['PHP_SELF'];
$title =  $_POST['title'];
$firstname =  $_POST['firstname'];
$surname =  $_POST['surname'];
$addline1 =  $_POST['addline1'];
$addline2 =  $_POST['addline2'];
$city =      $_POST['city'];
$postcode =  $_POST['postcode'];
$hphone =  $_POST['hphone'];
$email =  $_POST['email'];
[/code]
Am having a problem posting to this topic. let me see if i can send the script as an attachment

You need to consider putting all db info. in a separate file and sending it up as a header.
file:info.php


[code]<?
$dbservertype='mysql';
$servername='localhost';
$dbusername=whatever;
$dbpassword='whatever';
$dbname='whatever';

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>[/code]

Then your file form.php

[code]<?
include "info.php";
?>

<html><head><title>Order Form</title></head><body>

<form action="form2.php" 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>
   

First Name: <input type="text" name="firstname">

Surname: <input type="text" name="surname">

Address Line 1: <input type="text" name="addline1">

Address Line 2: <input type="text" name="addline2">

City:<input type="text" name="city">

Post Code: <input type="text" name="postcode">

Contact Number: <input type="text" name="hphone">

Email Address: <input type="text" name="email">

<input type="Submit" name=submit value=Submit>
</form>

<?php       
$self =      $_SERVER['PHP_SELF'];      // remove all those extra spaces ..see title below
$title = $_POST['title'];
$firstname =  $_POST['firstname'];
$surname =  $_POST['surname'];
$addline1 =  $_POST['addline1'];
$addline2 =  $_POST['addline2'];
$city =      $_POST['city'];
$postcode =  $_POST['postcode'];
$hphone =  $_POST['hphone'];
$email =  $_POST['email'];

$sql = "INSERT INTO 'cust_tbl' (title,fname,sname,adline1,adline2,city,postcode,hphone,email) VALUES
( '$title', "$firstname", "$surname", "$addline1", "$addline2", "$city", "$postcode", "$hphone", "$email" )";
// use single quotes - see $title)
  $result = mysql_query( $sql ) or die (mysql_error()) ; // error reporting is important and remove the @

echo "information entered" ;
// at this point , consider another sql to autoincrement the records as i had posted earlier.
?>
</body></html>[/code]


[code]$sql = "INSERT INTO 'cust_tbl' (title,fname,sname,adline1,adline2,city,postcode,hphone,email) VALUES
 ( "$title", "$firstname", "$surname", "$addline1", "$addline2", "$city", "$postcode", "$hphone", "$email" )";

[/code]

on the second part of the query after VALUES ("... you cannot use double quotes, change those to single quotes for each variable, the code below should not produce and parsing problems

[code]$sql = "INSERT INTO cust_tbl (title, fname, sname, adline1, adline2, city, postcode, hphone, email) VALUES
 ( '$title', '$firstname', '$surname', '$addline1', '$addline2', '$city', '$postcode', '$hphone', '$email' )";
[/code]

i can see that all of your variables are strings, but if they weren't you don't have to use any quotes in the query line like the postcode for US will not be a string and you can query it without any quotes.

The other thing is when you use double quotes "" you cannot use anymore double quotes inside of them and expect it to work.

Hope this help
Warning: mysql_query(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/www/dombar0.freehostia.com/form2.php on line 50

Warning: mysql_query(): A link to the server could not be established in /home/www/dombar0.freehostia.com/form2.php on line 50
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

This is the error recieved with swatisonne's help with the code. Im sure everything has been done that you suggested.

This is line 50:

[quote]$result = mysql_query( $sql ) or die (mysql_error()) ; [/quote]

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.