Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

simple php and mysql question.

Options
  • 25-04-2007 9:21pm
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    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 Posts: 11,980 ✭✭✭✭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 Posts: 1,987 ✭✭✭Ziycon


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


  • Registered Users Posts: 26,579 ✭✭✭✭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