Jump to content

[resolved] my first attempt to write to a database - entires not writing - help!


scotch33

Recommended Posts

Hi there,

I am writing my first sql app - when a visitor reaches a 'payment completed' page after a payment I am trying to write the transaction details to an orders database.

so far, I have created a test database, added a few entries and managed to link to them and display them - si I know the connection is working.  However when I try to write a new entry with the code below (basically - calling cookies set before the customer reroutes to the payment gateway), fails to write.  I have compared this code with the tutorial I am using to learn from, but can't see anything.  can anyone help?  Thanks!

[code]<?
$cust_name = $_COOKIE[name];
$cust_surname = $_COOKIE[surname];
$cust_address1 = $_COOKIE[address1];
$cust_address2 = $_COOKIE[address2];
$cust_town = $_COOKIE[town];
$cust_county = $_COOKIE[county];
$cust_postcode = $_COOKIE[postcode];
$cust_email = $_COOKIE[email];
$cust_phone = $_COOKIE[phone];
$unit_quantity = $_COOKIE[amount];

$sql_insert = "INSERT INTO dvd_orders SET
cust_name='$cust_name',
cust_surname='$cust_surname',
cust_address1='$cust_address1',
cust_address2='$cust_address2',
cust_town='$cust_town',
cust_county='$cust_county',
cust_postcode='$cust_postcode',
cust_email='$cust_email',
cust_phone='$cust_phone',
unit_quantity='$unit_quantity',
unit_cost='30',
";
if (@mysql_query($sql_insert)) {
echo "this has worked";
} else {
echo "this hasn't worked - bugger";
}
?>[/code]
It could be any number of things but suppressing errors (@) doesn't help. You need them on while developing. Try...

[code=php:0]
if (mysql_query($sql_insert)) {
    echo "this has worked";
} else {
    echo mysql_error();
}
[/code]
this is how i would do it:

[color=blue]$sql_insert = "INSERT INTO dvd_orders SET
cust_name='".$cust_name."',
cust_surname='".$cust_surname."',
cust_address1='".$cust_address1."',
cust_address2='".$cust_address2."',
cust_town='".$cust_town."',
cust_county='".$cust_county."',
cust_postcode='".$cust_postcode."',
cust_email='".$cust_email."',
cust_phone='".$cust_phone."',
unit_quantity='".$unit_quantity."',
unit_cost='".30."',[/color]
i think that shud do it if the problem is in the sql query, otherwise its your data from cookies
Thanks thorpe - I have made that change and am now getting the following -

You have an error in your SQL syntax near '' at line 13

I am about to see if r-it's code does the trick (thanks!) - but I have looked at the php page and line 13 is as follows -

<meta name="Author" content="Pearson Treehouse(c) 2006" />

not even anywhere near the database! willget back once i have tried alternative code.
[quote]but I have looked at the php page and line 13 is as follows -[/quote]

No. But, line 12 of your sql statement ends with a , when it shouldn't. Change your sql to...

[code=php:0]
$sql_insert = "INSERT INTO dvd_orders SET
cust_name='$cust_name',
cust_surname='$cust_surname',
cust_address1='$cust_address1',
cust_address2='$cust_address2',
cust_town='$cust_town',
cust_county='$cust_county',
cust_postcode='$cust_postcode',
cust_email='$cust_email',
cust_phone='$cust_phone',
unit_quantity='$unit_quantity',
unit_cost='30'
";
[/code]

PS; r-it's code is no different to yours. And in fact introduces another error.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.