Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

simple php and mysql question.

  • 25-04-2007 09:21PM
    #1
    Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭


    i'm trying to update a table i have on my mysql database on my server.

    basically it's a table that's holding information about a shopping cart.

    here's the table

    [php]
    CREATE TABLE cart (
    cart_id int(10) unsigned NOT NULL auto_increment,
    book varchar(100) default NULL,
    qty int(10) NOT NULL default '0',
    price float NOT NULL default '0',
    PRIMARY KEY (cart_id)
    )
    [/php]
    now what i want to be able to do is to add rows to this table for every item the user selects to put in their cart.

    this is what i have so far
    [php]
    //these variables only for this example etc.
    $item = "item1" ;
    $_SESSION += $_POST ;
    $price = 9.99 ;

    mysql_query("INSERT INTO cart(book, qty, price) VALUES('$item','$_SESSION','$price')") or die(mysql_error());
    [/php]
    i'm not getting an sql error but i'm getting this php error

    [php][Wed Apr 25 21:00:20 2007] [error] PHP Parse error: syntax error, unexpected T_BAD_CHARACTER, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hsphere/local/home/ncremins/neilcremins.com/cart.php on line 109
    [/php]
    what's the best way to construct mysql_query statements whilst have php variables as parameters?

    just so you know i'm not coding this for it to be put to use just learning the skills behind it :D

    any help will be greatly appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 12,025 ✭✭✭✭Giblet


    [php]("insert into cart(book, qty, price) values (\"" . $item . "\",\"" . $_Session . "\",\"" . $price"\",\"" . $email . "\",\"" . $uname .");[/php]

    might need escape characters too, I can't check that though :)


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    Try this:
    [PHP]mysql_query("insert into cart(book, qty, price) values ('".$item."','".$_SESSION."','".$price."'") or die(mysql_error());[/PHP]


  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    Ziycon wrote:
    Try this:
    [php]mysql_query("insert into cart(book, qty, price) values ('".$item."','".$_SESSION."','".$price."'") or die(mysql_error());[/php]

    works perfectly, except i had the stick in a closing ) for the values(....) statement i assume it was a typo ;).

    thanks guys for your help, i'll get there yet :P


  • Closed Accounts Posts: 593 ✭✭✭McSandwich


    The problem is that you are trying to insert strings into int and float fields.

    This should work - I've removed the quotes surrounding the qty and price values.

    [php]
    mysql_query("INSERT INTO cart(book, qty, price)
    VALUES('$item', $_SESSION, $price)")
    or die(mysql_error());
    [/php]


Advertisement