Page 1 of 1

newbie help please!

Posted: Tue Sep 06, 2005 9:34 am
by tom.cull
Hi, have just started learning php/mysql today and am trying to creat a simple form to input info into a database. From the internet I have done this so far:

Code: Select all

<body>

<form enctype="multipart/form-data" method="post"  action="<?php echo $PHP_SELF ?>">
Contact Name<br>
<input type="Text" name="contact_name" size="25">
<br>
Contact Email<br>
<input type="Text" name="contact_email" size="25">
<br>
Contact Phone<br>
<input type="Text" name="contact_phone" size="25">
<br>
Car Make<br>
<input type="Text" name="car_make" size="25">
<br>
Car Model<br>
<input type="File" name="car_model" size="25">
<br>
Car Year<br>
<input type="File" name="car_year" size="25">
<br>
Car Mileage<br>
<input type="File" name="car_mileage" size="25">
<br>
Car Price<br>
<input type="File" name="car_price" size="25">
<br>
Car Type<br>
<input type="File" name="car_type" size="25">
<br>
Car Info<br>
<input type="File" name="car_info" size="25">
<br>
Car pbracket<br>
<input type="File" name="car_pbracket" size="25">
<br><br>
<input type="submit" name="submit" value="Upload">
</form> 




<?php

if ($submit) {

$username="xxxx";
$password="xxxx";
$database="usedcars";

$contact_name=$_POST['contact_name'];
$contact_email=$_POST['contact_email'];
$contact_phone=$_POST['contact_phone'];
$car_make=$_POST['car_make'];
$car_model=$_POST['car_model'];
$car_year=$_POST['car_year'];
$car_mileage=$_POST['car_mileage'];
$car_price=$_POST['car_price'];
$car_type=$_POST['car_type'];
$car_info=$_POST['car_info'];
$car_pbracket=$_POST['car_pbracket'];



mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO $database "  "VALUES ('$contact_name','$contact_email','$contact_phone','$car_make,$car_model,$car_year,$car_mileage,$car_price,$car_type,$car_info,$car_pbracket')";
mysql_query($query);

mysql_close();
?>


</body>
when i upload this to my server and run the file I get :

Parse error: parse error, unexpected '\"' in /home/newsstuf/public_html/cartest/add_data.php on line 75

i cant see an error - please help!!!

thanks



Posted: Tue Sep 06, 2005 9:38 am
by s.dot

Code: Select all

$query = "INSERT INTO $database "  "VALUES ('$contact_name','$contact_email','$contact_phone','$car_make,$car_model,$car_year,$car_mileage,$car_price,$car_type,$car_info,$car_pbracket')";
to

Code: Select all

$query = "INSERT INTO $database VALUES ('$contact_name','$contact_email','$contact_phone','$car_make,$car_model,$car_year,$car_mileage,$car_price,$car_type,$car_info,$car_pbracket')";

Posted: Tue Sep 06, 2005 9:42 am
by CoderGoblin
Would be easier to debug if we knew which line was 75 (I hate counting :wink: )

I think you have an error in your SQL.. It should be...

Code: Select all

$query="INSERT INTO $database 
VALUES ('$contact_name','$contact_email','$contact_phone','$car_make,$car_model,
               $car_year,$car_mileage,$car_price,$car_type,$car_info,$car_pbracket')";
You may also have problems with

Code: Select all

mysql_connect(localhost,$username,$password);
Surely this should be

Code: Select all

mysql_connect('localhost',$username,$password);
or a variable ?

Edit: Damn hate it when someone beats me to it...

Posted: Tue Sep 06, 2005 9:57 am
by tom.cull
hi - thanksfor replies.

OK - i have tried both suggestions, still no luck.

It now looks liek this:

Code: Select all

<?php

if ($submit) {

$username="********";
$password="********";
$database="usedcars";

$contact_name=$_POST['contact_name'];
$contact_email=$_POST['contact_email'];
$contact_phone=$_POST['contact_phone'];
$car_make=$_POST['car_make'];
$car_model=$_POST['car_model'];
$car_year=$_POST['car_year'];
$car_mileage=$_POST['car_mileage'];
$car_price=$_POST['car_price'];
$car_type=$_POST['car_type'];
$car_info=$_POST['car_info'];
$car_pbracket=$_POST['car_pbracket'];



mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO $database VALUES ('$contact_name','$contact_email','$contact_phone','$car_make,$car_model,$car_year,$car_mileage,$car_price,$car_type,$car_info,$car_pbracket')"; 

$result = mysql_query($sql);


mysql_close();
?>

</body>
</html>
i now get :

Parse error: parse error, unexpected $ in /home/newsstuf/public_html/cartest/add_data.php on line 81

line 81 is the very last line

Code: Select all

</html>
!!??!


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Sep 06, 2005 10:03 am
by neophyte
In your query:

Code: Select all

'$contact_phone','$car_make,$car_model,
That's going to give you a headache.

For the unexpected $ you for got to close your if($submit){ statement.

Posted: Tue Sep 06, 2005 10:05 am
by feyd
I took the liberty of removing your username and password from your code.. you may want to change both, soon.

Posted: Tue Sep 06, 2005 10:24 am
by tom.cull
Well it all appeared to work after the last batch of changes, the page loaded, gave me an error about connecting to db, I then correted the name of the db and it worked OK. I got a form on the page without problem - when you hit sumbit, the form jsut empties and re-appears - and no records are added to db.

am i being really stupid and missing something...?

thanks for all the help so far!

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form enctype="multipart/form-data" method="post"  action="<?php echo $PHP_SELF ?>">
Contact Name<br>
<input type="Text" name="contact_name" size="25">
<br>
Contact Email<br>
<input type="Text" name="contact_email" size="25">
<br>
Contact Phone<br>
<input type="Text" name="contact_phone" size="25">
<br>
Car Make<br>
<input type="Text" name="car_make" size="25">
<br>
Car Model<br>
<input type="Text" name="car_model" size="25">
<br>
Car Year<br>
<input type="Text" name="car_year" size="25">
<br>
Car Mileage<br>
<input type="Text" name="car_mileage" size="25">
<br>
Car Price<br>
<input type="Text" name="car_price" size="25">
<br>
Car Type<br>
<input type="Text" name="car_type" size="25">
<br>
Car Info<br>
<input type="Text" name="car_info" size="25">
<br>
Car pbracket<br>
<input type="Text" name="car_pbracket" size="25">
<br><br>
<input type="submit" name="submit" value="Upload">
</form> 

<?php

if ($submit) {

$username="xxxx";
$password="xxxx";
$database="xxxx";

$contact_name=$_POST['contact_name'];
$contact_email=$_POST['contact_email'];
$contact_phone=$_POST['contact_phone'];
$car_make=$_POST['car_make'];
$car_model=$_POST['car_model'];
$car_year=$_POST['car_year'];
$car_mileage=$_POST['car_mileage'];
$car_price=$_POST['car_price'];
$car_type=$_POST['car_type'];
$car_info=$_POST['car_info'];
$car_pbracket=$_POST['car_pbracket'];



mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO $database VALUES ('$contact_name','$contact_email','$contact_phone','$car_make',$'car_model',$'car_year',$'car_mileage',$'car_price',$'car_type',$'car_info',$'car_pbracket')"; 

$result = mysql_query($sql);

}

mysql_close();
?>

</body>
</html>
[/i]

Posted: Tue Sep 06, 2005 10:34 am
by feyd

Code: Select all

'$contact_phone','$car_make',$'car_model',$'car_year',$'car_mileage'
*elmer fudd voice* wook vewy cwouswy at yahu pwacement of dowah signs. :)

Posted: Tue Sep 06, 2005 10:40 am
by tom.cull
thanks - ok they are now all '$xxx' as opposed to $'xxx' - but the thing still wont work!

Posted: Tue Sep 06, 2005 10:44 am
by feyd
if you do..

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
does anything of the output change? what does it say?

Posted: Tue Sep 06, 2005 10:47 am
by neophyte
echo $query; Copy and paste the query directly into phpMyAdmin or run it directly againt the db.

You might try this too:

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
Hope that helps.

Posted: Tue Sep 06, 2005 10:48 am
by tom.cull
the form now loads with:

Query was empty

at the bottom - if you enter data and submitt, the page refreshes, the fields empty and the " Query was empty" message returns.

Posted: Tue Sep 06, 2005 10:53 am
by feyd
your query is $query, yet you call mysql_query() with $sql

Posted: Tue Sep 06, 2005 5:16 pm
by tom.cull
thanks - I think thats fixed the query part - it now tells me unable to select database - I guess thats jsut down to me trying to name the database properlly etc.