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

MySQL error

Options
  • 04-03-2008 7:21pm
    #1
    Registered Users 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 Posts: 68,317 ✭✭✭✭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 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 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 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 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 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 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