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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.