newbie help please!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

newbie help please!

Post 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


User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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')";
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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...
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post 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]
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I took the liberty of removing your username and password from your code.. you may want to change both, soon.
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

thanks - ok they are now all '$xxx' as opposed to $'xxx' - but the thing still wont work!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your query is $query, yet you call mysql_query() with $sql
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post 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.
Post Reply