#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    64
    Rep Power
    1

    Unknown column error


    I am trying to do an insert query but it always fails on me.

    Code:
    mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO article_acc (uid, aid, user) VALUES($userid, $article_number, ".$CURUSER['username'].")") or sqlerr(__FILE__, __LINE__);
    The article_acc table contains the uid, aid and user fields.
    Everytime i run the query i get Unknown column 'TestAc****' in 'field list'
    That is the username i am using for this, but it should't say it is a column because that data should go in the user column.

    the userid goes into the uid, the article_number goes into the aid but when the username needs to go into the user column it will bork that error.
    Any suggestions?
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Use a prepared statement and the problem will go away.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    64
    Rep Power
    1
    What do you mean by that?
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    By what part? By using a prepared statement? mysqli can do prepared statements. Or by the problem going away? Your problem has to do with putting data into the query directly, and since prepared statements don't put data into the query directly in the first place, the problem... goes away.
  8. #5
  9. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,734
    Rep Power
    4288
    Originally Posted by pwpx2
    Any suggestions?
    it's a string... put single quotes around it
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    64
    Rep Power
    1
    single quotes didn't do the trick
    about prepared statements i need to google about them.. never did one
  12. #7
  13. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,734
    Rep Power
    4288
    Originally Posted by pwpx2
    single quotes didn't do the trick
    perhaps your single quotes were in the wrong place

    try it like this:
    Code:
    INSERT INTO article_acc (uid, aid, user) VALUES($userid, $article_number, '".$CURUSER['username']."' )
    here is the relevant part, close up -- '".$CURUSER['username']."'

    Comments on this post

    • pwpx2 agrees
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    64
    Rep Power
    1
    Hmm, fist time didn't work. Now it is perfect.
    Thank you.
    p.s Must've been my stupid wireless keyboard. It misses some keys when pressing.

IMN logo majestic logo threadwatch logo seochat tools logo