syntax error but where?

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

syntax error but where?

Post by tom.cull »

I have a edititem page where I can lookup and edit records from my db.

When you click on save it is passed through to a saveitem.php - this seems to work with a successful "Successfully saved the entry." message however nothing updates!
help.
thanks

Code: Select all

<html>
<body>


<?php

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

   // saving script
   
   // connect to the server
   mysql_connect( 'localhost', $username, $password )
      or die( "Error! Could not connect to database: " . mysql_error() );
   
   // select the database
   mysql_select_db( $database )
      or die( "Error! Could not select the database: " . mysql_error() );
   
   // get the variables from the URL request string
   $id = $_REQUEST['id'];
   $contact_nfirst = $_REQUEST['contact_nfirst'];
   $contact_nlast = $_REQUEST['contact_nlast'];
   $contact_email = $_REQUEST['contact_email'];
   $contact_phone = $_REQUEST['contact_phone'];
   $car_make = $_REQUEST['car_make'];
   $car_model = $_REQUEST['car_model'];
   $car_year = $_REQUEST['car_year'];
   $car_mileage = $_REQUEST['car_mileage'];
   $car_price = $_REQUEST['car_price'];
   $car_type = $_REQUEST['car_type'];
   $car_info = $_REQUEST['car_info'];
   $car_pbracket = $_REQUEST['car_pbracket'];

   // if $id is not defined, we have a new entry, otherwise update the old entry
   if( $id )
   {
      $query = "UPDATE table SET contact_nfirst='$contact_nfirst', contact_nlast='$contact_nlast', contact_email='$contact_phone', car_make='$car_make', car_model='$car_model', car_year='$car_year', car_mileage='$car_mileage', car_price='$car_price', car_type='$car_type', car_info='$car_info', car_pbracket='$car_pbracket' WHERE 'id'='$id'";
   }
   else
   {
      $query = "INSERT INTO newsstuf_usedcars ( 'contact_nfirst','contact_nlast','contact_email','contact_phone','car_make','car_model','car_year','car_mileage','car_price','car_type','car_info','car_pbracket' ) 
         VALUES ( '$contact_nfirst','$contact_nlast','$contact_email','$contact_phone','$car_make','$car_model','$car_year','$car_mileage','$car_price','$car_type','c$ar_info','$car_pbracket' )";   
   }

   // save the info to the database
   $results = mysql_query( $query );

   // print out the results
   if( $results )
   {
      echo( "Successfully saved the entry." );
   }
   else
   {
      die( "Trouble saving information to the database: " . mysql_error() );
   }
   
?>

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

Post by feyd »

your update query is faulty. Try this:

Code: Select all

$query = "UPDATE table SET contact_nfirst='$contact_nfirst', contact_nlast='$contact_nlast', contact_email='$contact_phone', car_make='$car_make', car_model='$car_model', car_year='$car_year', car_mileage='$car_mileage', car_price='$car_price', car_type='$car_type', car_info='$car_info', car_pbracket='$car_pbracket' WHERE `id`='$id'";
You had single quotes around the field named id; that's a string to SQL, not a column reference.
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

hi - thanks for that - i have ammended but am still having same problem??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the insert query has the same problem... which isn't working? echo out $query before you call mysql_query()
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

sorry - new to all this - how to I echo out $query?
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

echo $query;
:P
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

hi thanks.

ok, i echo'd the query and now get the below when saving the data:
UPDATE table SET contact_nfirst='Tom', contact_nlast='cull', contact_email='', car_make='', car_model='', car_year='', car_mileage='', car_price='', car_type='', car_info='', car_pbracket='' WHERE `id`='1'Successfully saved the entry.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Looks like your variables aren't being passed. I've never used $_REQUEST but try this:

Code: Select all

$var = $_GET['var'];
Since they are coming from the URL.
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.
tom.cull
Forum Commoner
Posts: 32
Joined: Tue Sep 06, 2005 9:31 am

Post by tom.cull »

perfet - thanks - that did the trick
Post Reply