davidcriniti Posted February 25, 2012 Share Posted February 25, 2012 Hi, I'm working with sessions for the first time and have been able to put a member's id into the database. However, when I try and put their display name into the database I got a message saying Unknown column 'xyz' in 'field list' xyz was the display name for the session. Just can't seem to see what I'm doing wrong. In the code below, I even tried changing $_SESSION['member_display_name'] to $_SESSION['member_id'], in which case the correct id was inserted into both the member_id and the member_display_name fields in the database, so I know the table is set up correctly. Any tips? //**********************SEND TO DATABASE**************************** include 'mysql_connect.php'; $query = "INSERT INTO uploads (date, member_id, upload_name, upload_title, upload_type, subject, topic, year, status, keywords, description, member_display_name, firstname, lastname)" . "VALUES (NOW(), ".$_SESSION['member_id'] . ",'$upload_nameX', '$upload_title', '".$EXPLODED_STRING[1]."', '$subject', '$topic', '$year', '$status', '$keywords', '$description', ".$_SESSION['member_display_name'] . ", '$firstname', '$lastname')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); $upload_id = mysql_insert_id(); //***********************END OF DATABASE CODE*********************** Quote Link to comment https://forums.phpfreaks.com/topic/257741-session-info-into-database/ Share on other sites More sharing options...
trq Posted February 25, 2012 Share Posted February 25, 2012 Strings need to be surrounded by quotes within SQL. Quote Link to comment https://forums.phpfreaks.com/topic/257741-session-info-into-database/#findComment-1321024 Share on other sites More sharing options...
davidcriniti Posted February 25, 2012 Author Share Posted February 25, 2012 I thought I did surround it with quotes. Did I do so incorrectly? ...here's the relevant bit of code, with the preceding and following fields. I tried a few variations of quote placements after your comment, but to no avail I'm afraid. '$description', ".$_SESSION['member_display_name'] . ", '$firstname', Quote Link to comment https://forums.phpfreaks.com/topic/257741-session-info-into-database/#findComment-1321025 Share on other sites More sharing options...
Pikachu2000 Posted February 25, 2012 Share Posted February 25, 2012 That isn't enclosed in quotes. The double quote ends the quoted string, then the full stop concatenates the array element to the string, then the next full stop concatenates the following quoted string. $_SESSION['member_display_name'] = 'string'; $query = "VALUES ( '$description', ".$_SESSION['member_display_name'] . ", '$firstname')"; // ABOVE WOULD BE THE SAME AS BELOW $query = "VALUES ( '$description', string, '$firstname')"; That can be made less confusing and easier to read by simply enclosing any associative array elements in curly braces. $query = "VALUES ( '$description', '{$_SESSION['member_display_name']}', '$firstname')"; Quote Link to comment https://forums.phpfreaks.com/topic/257741-session-info-into-database/#findComment-1321027 Share on other sites More sharing options...
davidcriniti Posted February 25, 2012 Author Share Posted February 25, 2012 Thanks Pikachu2000, Solution worked perfectly and the explanation helped me understand where I was going wrong. Thanks again for your time, Dave Quote Link to comment https://forums.phpfreaks.com/topic/257741-session-info-into-database/#findComment-1321030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.