Page 1 of 1

syntax error but where?

Posted: Wed Sep 07, 2005 2:33 pm
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]

Posted: Wed Sep 07, 2005 2:37 pm
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.

Posted: Wed Sep 07, 2005 2:48 pm
by tom.cull
hi - thanks for that - i have ammended but am still having same problem??

Posted: Wed Sep 07, 2005 2:51 pm
by feyd
the insert query has the same problem... which isn't working? echo out $query before you call mysql_query()

Posted: Thu Sep 08, 2005 2:29 am
by tom.cull
sorry - new to all this - how to I echo out $query?

Posted: Thu Sep 08, 2005 2:41 am
by feyd

Code: Select all

echo $query;
:P

Posted: Thu Sep 08, 2005 5:29 am
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.

Posted: Thu Sep 08, 2005 6:21 am
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.

Posted: Thu Sep 08, 2005 8:25 am
by tom.cull
perfet - thanks - that did the trick