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.

MySQL error

  • 04-03-2008 07:21PM
    #1
    Registered Users, Registered Users 2 Posts: 45


    Error:
    Unknown column 'Jordan' in 'where clause'

    My code:

    $v=UPLOAD_DIR.$_SESSION.'/'.$now.$_SESSION.'-'.$file;

    //copy image to database

    $sql = "SELECT user_id FROM user WHERE username = ".$_SESSION."";
    $queryresult = mysql_query($sql)
    or die (mysql_error());
    if (mysql_num_rows($queryresult) > 0) {
    $row=mysql_fetch_assoc($queryresult);
    $userid=$row;
    }

    $resins=mysql_query("INSERT INTO gallery (user_id, image_name) VALUES ($userid,'$v'")
    or die("Error in INSERT: ".mysql_error());




    The only place I can see it is getting the name 'Jordan' is from the session name .$_SESSION.

    Can anyone help with this...


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    This line here:
    $sql = "SELECT user_id FROM user WHERE username = ".$_SESSION."";

    Is generating a line like

    SELECT user_id FROM user WHERE username = Jordan;

    Strings need to be enclosed in Single quotes, so the above should look like:


    SELECT user_id FROM user WHERE username = 'Jordan';


  • Registered Users, Registered Users 2 Posts: 45 Jordan79


    Thanks Seamus.
    I have been working on my code:

    [PHP]$sql = "SELECT `user_id` FROM `user` WHERE `username` = '".$_SESSION."'";
    $queryresult = mysql_query($sql)
    or die (mysql_error());
    if (mysql_num_rows($queryresult) > 0) {
    $row=mysql_fetch_assoc($queryresult);
    $userid=$row;
    }

    $resins=mysql_query("INSERT INTO gallery ('user_id', 'image_name') VALUES ($userid,'$v')")
    or die("Error in INSERT: ".mysql_error());[/PHP]

    But now getting this error:
    ""Error in INSERT: 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 ''user_id', 'image_name') VALUES ('5','C:\htdocs\PhotoABC\upload_test\Jordan/2008' at line 1"

    I think I am nearly there because my VALUES seem correct when listed in the error but again I think I have a syntax error.
    Wonder do u have any idea?


  • Registered Users, Registered Users 2 Posts: 45 Jordan79


    Its working now
    Thanks for all your help
    Here is the working code

    [PHP]$sql = "SELECT `user_id` FROM `user` WHERE `username` = '".$_SESSION."'";
    $queryresult = mysql_query($sql)
    or die (mysql_error());
    if (mysql_num_rows($queryresult) > 0) {
    $row=mysql_fetch_assoc($queryresult);
    $userid=$row;
    }

    $resins=mysql_query("INSERT INTO gallery (`user_id`, `image_name`) VALUES ('$userid','$v')")
    or die("Error in INSERT: ".mysql_error());[/PHP]

    Column names have to be ` and not '
    Found it in a book I have :)


  • Closed Accounts Posts: 81 ✭✭dzy


    You don't have to quote the field names in the SQL.

    So it would be
    [COLOR=#000000][COLOR=#dd0000]"INSERT INTO gallery (user_id, image_name) VALUES ($userid,'$v')"[/COLOR][/COLOR]
    


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    dzy wrote: »
    You don't have to quote the field names in the SQL.

    So it would be
    [COLOR=#000000][COLOR=#dd0000]"INSERT INTO gallery (user_id, image_name) VALUES ($userid,'$v')"[/COLOR][/COLOR]
    
    Yep it safer not to have anything around the column/field names. Very hard to see different between a ` and a ' sometimes - and could drive you mad!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 45 Jordan79


    Yeah I had it tried a lot of ways but kept getting syntax errors. It works now so think I will just go with it.
    I have all my DB storage PHP written now so I am happy :)

    oh all this code is for my college project so thanks again for all the help

    :)


  • Closed Accounts Posts: 198 ✭✭sh_o


    debugging tip..... What can help when doing these types of things is to output the $sql part in the die statement so when looking at the sql that is produced, you can copy and paste this into a sql worksheet.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    sh_o wrote: »
    debugging tip..... What can help when doing these types of things is to output the $sql part in the die statement so when looking at the sql that is produced, you can copy and paste this into a sql worksheet.
    [php]mysql_query(.......) or die (mysql_errorno())[/php] - Yep useful alright


  • Registered Users, Registered Users 2 Posts: 1,045 ✭✭✭Bluefrog


    I actually think it can be helpful to quote the field names even though it's not required - it stops you generating errors if you accidentally use reserved keywords as column names.


Advertisement