SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error using 'where' in mysql query

    Hi,

    I am using this query:

    PHP Code:
        $sql "INSERT INTO cart (emailaddress, firstname, lastname, address1, address2) VALUES ('$emailaddress', '$firstname', '$lastname', '$address1', '$address2') where cookieid = '" GetCartId() . "')"
    and am getting this error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where cookieid = '3na0c1frfs2uo3gl462ajm0c1')' at line 1

    I want to add the data provided where cookieid=users cookie. But it says the error above!

    Any ideas?

    Matt.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You can't use WHERE in an INSERT query. What are you trying to achieve?
    Did you maybe mean to UPDATE instead of INSERT?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Guru r2d2's Avatar
    Join Date
    Dec 2003
    Location
    In my van, fool!
    Posts
    646
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, I noticed you have an excess close bracket after your WHERE clause.

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Insert seemed logical to me.

    What happens is someone adds something to the basket. And when they do it saves their cookie value and product and quantity they have bought.

    When they click proceed on the basket page, they then enter their name, telephone, address, etc. This information needs entering in the same row in the MySQL table as the their basket contents and cookie value. That is why I am adding this data where the cookie in the table matches the value in their computer.

    I have tried your update idea. It does not work but may be it incorrectly coded:

    PHP Code:
    $sql "update cart set (emailaddress, firstname, lastname, address1, address2) to ('$emailaddress', '$firstname', '$lastname', '$address1', '$address2') where cookieid = '" GetCartId() . "'"
    What should I use? I believe I need to use WHERE but if i cannot use it with INSERT what can be done!?

    Matt.

  5. #5
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post

    I have tried your update idea. It does not work but may be it incorrectly coded:
    Your syntax is incorrect. Did you look up how to set multiple values in an update query?

    Google "update set sql" and you'll get lots of examples showing how to fix your update query.

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried this instead:

    PHP Code:
    $sql "update cart set emailaddress='$emailaddress' where cookieid = '" GetCartId() . "'"
    and it works. But how do I get multiple entries!?!

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    UPDATE bla SET a=1, b=2, c=3 WHERE something='somethingelse'
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    got it. works like a dream,

    Matt.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •