Forum Moderators: coopster

Message Too Old, No Replies

Problems with INSERT INTO statement

         

NeilB84

1:49 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



Hi I'm new to PHP, and I'm having problems getting this form to work to insert data into a database. It is saying -

Parse error: syntax error, unexpected T_STRING in C:\apachefriends\xampp\htdocs\reserve.php on line 24

// If the form has been submitted
<?php

if(isset($_GET['commented']))
{

// Tell the user it has been submitted (optional)
echo('Thank you for your reservation.');

// Set Mysql Variables
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'books';
$table = 'customers';

$surname=$_GET['name'];
$forename=$_GET['Fname'];
$tel=$_GET['Tel'];
$email=$_GET['email'];

mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$add_all = INSERT INTO $table values('$surname','$forename','$tel','$email');
mysql_query($add_all) or die(mysql_error());
}
else
{

// If the form has not been submitted, display it!
?>
<form method='get' action='<? echo'$PHP_SELF';?>'>
<br><font face="Verdana"><font size=-1>ISBN -</font></font>
<br><font face="Verdana"><font size=-1>Price -</font></font>
<p><font face="Verdana"><font size=-1>Surname</font></font><input NAME = "name" TYPE = "text" SIZE = "25" MAXLENGTH = "20">
<p><font face="Verdana"><font size=-1>Forename</font></font><input NAME = "FName" TYPE = "text" SIZE = "25" MAXLENGTH = "20">
<p><font face="Verdana"><font size=-1>Tel N.o.</font></font><input NAME = "Tel" TYPE = "text" SIZE = "25" MAXLENGTH = "12">
<p><font face="Verdana"><font size=-1>e-Mail address</font></font><input NAME = "email" TYPE = "text" SIZE = "25" MAXLENGTH = "35">
<p><font face="Verdana"><font size=-1>Date picking up</font></font><input NAME = "date" TYPE = "text" SIZE = "5" MAXLENGTH = "8">
<input type='submit' value='Reserve your book'>
</form>
<?
}
?>

Any ideas as I can't figure it out?

mcibor

2:21 pm on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$add_all has to be a string. Just put it in a quote:

$add_all = "INSERT INTO $table values('$surname','$forename','$tel','$email')";

If it doesn't work tell me!

Best regards

Michal Cibor

And Welcome to WebMasterWorld!

NeilB84

3:50 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



Thanks a lot it finally loads now, but I have a new problem as it is not actually inserting the data to the database. :S

mcibor

6:45 pm on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post the problem here, or create a new topic:)

See you!

Michal Cibor

NeilB84

8:50 am on Apr 10, 2005 (gmt 0)

10+ Year Member



Right, so here's the coding -

// If the form has been submitted
<?php

if(isset($_GET['commented']))
{

// Tell the user it has been submitted (optional)
echo('Thank you for your reservation.');

// Set Mysql Variables
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'books';
$table = 'customers';

$surname=$_GET['name'];
$forename=$_GET['Fname'];
$tel=$_GET['Tel'];
$email=$_GET['email'];

mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$add_all = "INSERT INTO $table values('$surname','$forename','$tel','$email')";
mysql_query($add_all) or die(mysql_error());
}
else
{

// If the form has not been submitted, display it!
?>
<form method='get' action='<? echo'$PHP_SELF';?>'>
<br><font face="Verdana"><font size=-1>ISBN -</font></font>
<br><font face="Verdana"><font size=-1>Price -</font></font>
<p><font face="Verdana"><font size=-1>Surname</font></font><input NAME = "name" TYPE = "text" SIZE = "25" MAXLENGTH = "20">
<p><font face="Verdana"><font size=-1>Forename</font></font><input NAME = "FName" TYPE = "text" SIZE = "25" MAXLENGTH = "20">
<p><font face="Verdana"><font size=-1>Tel N.o.</font></font><input NAME = "Tel" TYPE = "text" SIZE = "25" MAXLENGTH = "12">
<p><font face="Verdana"><font size=-1>e-Mail address</font></font><input NAME = "email" TYPE = "text" SIZE = "25" MAXLENGTH = "35">
<p><font face="Verdana"><font size=-1>Date picking up</font></font><input NAME = "date" TYPE = "text" SIZE = "5" MAXLENGTH = "8">
<input type='submit' value='Reserve your book'>
</form>
<?
}
?>

Everything seems fine, the database and table are correct and the hostname, username and password are correct too as I have used this on another page.

jatar_k

5:35 pm on Apr 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is it hitting the proper part of the if and echoing 'Thank you for your reservation.'?

is it returning any errors from your die's?

have you tried echoing your insert query to see if it is properly constructed and then pasting that into the command line or something like phpmyadmin to test?

NeilB84

9:35 am on Apr 11, 2005 (gmt 0)

10+ Year Member



is it hitting the proper part of the if and echoing 'Thank you for your reservation.'?
No it's not echoing that, I think the problem lies in where I've put action=$PHP_Self, what would I have to put thier instead?

is it returning any errors from your die's?
No errors from the dies

mcibor

11:55 am on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem lies in the insert statement. I recon there are more columns than just 4. What you have to do is to correct it into:

$add_all = "INSERT INTO $table (surname, forename, tel, email) VALUES('$surname','$forename','$tel','$email')";

I would also do

$email = add_slashes($_GET["surname"]); to be on the safer side

Best regards
Michal Cibor

jatar_k

4:23 pm on Apr 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the thing I dont see is why you are testing

if(isset($_GET['commented']))

I don't see that represented anywhere. I figured there was nothing from the dies. It is not that the insert doesn't work but my guess is it just isn't ending up in the right part of the if statement.

ok, so let's fix that test. First thing is I would change your method to post in your form, you don't really want to pass all of that jun in the url, better posted. You also will want to change your submit button a little from

<input type='submit' value='Reserve your book'>
to
<input type='submit' name='reserve' value='Reserve your book'>

adding the name will allow for you to test for it's existence in the $_POST array. You also have an error in your action, this should be giving you a 404.

<form method='get' action='<? echo'$PHP_SELF';?>'>

with the single quotes around the php_self it shouldn't even resolve the variable. When you echo vars only they don't need to be surrounded by any type of quotes, if you are mixing string data and vars and want them to resolve then you need to surround them with double quotes. You form tag should look like so (with the post change as well)

<form method='post' action='<? echo $_SERVER['PHP_SELF'];?>'>

that is also using the $_SERVER array instead of the register_globals on version like $PHP_SELF, ultimately you will need to turn off register_globals anyway. Nopw when you test instead of

if(isset($_GET['commented'])) {
use
if(isset($_POST['reserve'])) {

now we are testing for the existence of the submit button in the $_POST array.

try that