mallen Posted September 22, 2007 Share Posted September 22, 2007 I have this form that I want to enter data. But I want it to read a session variable called "user_id" And when the form is submitted it enters it in the database corresponding to the correct user_id. So I figure I needed to tweek my code to say WHERE $_SESSION["user_id"]; but I can't get the SQL statement right. $query = "INSERT INTO users (user_id, title, description, images, address, price, contact, phone) VALUES (0,'{$_POST['title']}', '{$_POST ['description']}','{$image_name}', '{$_POST ['address']}', '{$_POST ['price']}', '{$_POST ['contact']}', '{$_POST ['phone']}')"; Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/ Share on other sites More sharing options...
cooldude832 Posted September 22, 2007 Share Posted September 22, 2007 Insert Into is for insertion of a new record to that table, not the modification of an existing, you want to you use the Update type of query in which it changes certain values of certain fields based on certain criteria. Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353005 Share on other sites More sharing options...
rarebit Posted September 22, 2007 Share Posted September 22, 2007 Or are you wanting the id it was inserted at? $id = mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353007 Share on other sites More sharing options...
mallen Posted September 22, 2007 Author Share Posted September 22, 2007 Maybe I wasn't clear. The user will not have these records so i won't be updating them. Its a form for them to enter property for sale. So most of these fields will be blank. Say user 1 has name, email, age ect. But the rest of the records are blank. I am just entering in the house they are selling ect. I want to enter it into the correct record. Like if the user id is 1, and the session id is telling it is 1, then enter the data in record 1, do not make a new record. Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353049 Share on other sites More sharing options...
gromer Posted September 22, 2007 Share Posted September 22, 2007 Maybe I wasn't clear. The user will not have these records so i won't be updating them. Its a form for them to enter property for sale. So most of these fields will be blank. Say user 1 has name, email, age ect. But the rest of the records are blank. I am just entering in the house they are selling ect. I want to enter it into the correct record. Like if the user id is 1, and the session id is telling it is 1, then enter the data in record 1, do not make a new record. So does the user have a row in the database already? If so, you need to use an UPDATE command. UPDATE Table SET something = input AND somethingelse = moreinput WHERE user_id = $_SESSION['user_id']; Sounds like the database design is kinda inefficient from what I'm reading. Do you have a users table and a properties table? Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353062 Share on other sites More sharing options...
mallen Posted September 22, 2007 Author Share Posted September 22, 2007 Yes I have a users table. That is where I am entering the data. Does this look better? $query = "UPDATE phpbb_users SET(title, description, images, address, price, contact, phone) VALUES (0,'{$_POST['title']}', '{$_POST ['description']}','{$image_name}', '{$_POST ['address']}', '{$_POST ['price']}', '{$_POST ['contact']}', '{$_POST ['phone']}' WHERE user_id=$_SESSION['user_id'] " ; Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353065 Share on other sites More sharing options...
gromer Posted September 22, 2007 Share Posted September 22, 2007 Yes I have a users table. That is where I am entering the data. Does this look better? $query = "UPDATE phpbb_users SET(title, description, images, address, price, contact, phone) VALUES (0,'{$_POST['title']}', '{$_POST ['description']}','{$image_name}', '{$_POST ['address']}', '{$_POST ['price']}', '{$_POST ['contact']}', '{$_POST ['phone']}' WHERE user_id=$_SESSION['user_id'] " ; Not sure if you need all the { and }, but that should do it. So each user can only have one property for sale at a time? Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353071 Share on other sites More sharing options...
mallen Posted September 22, 2007 Author Share Posted September 22, 2007 Yes just one property for now. Maybe its not working because I am not listing all the other fields. See I am adapting a huge phpbb forum database. So if they register in the forum or on the site its all going to one database. I figured it would be easier to add extra columns to the users table since its just what they are selling, ect. The reason for all the {''} is this form uploads the info, uploads an image, changes its name and stores it. I am trying to put two things together since they could come from the either side, the forum or the web site. Does that make sense? There are 30 tables in the database and inside the users table there are about 30 fields. So I just added a few to the users table. Maybe I should make a sellers table? Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353080 Share on other sites More sharing options...
JJohnsenDK Posted September 22, 2007 Share Posted September 22, 2007 you should differently make a sellers table! then you also could keep record of all your sells and which user you bought what ever you sell.... the table should look that this: sellers: sellers_id = 1 users_id = to the user who brought something, e.g. user 4 item_id = if any its as simple as that and for the record, when using the INSERT or UPDATE design it this way: INSERT INTO table SET user_id = 1, sellers = 1 WHERE user_id = SESSION Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353094 Share on other sites More sharing options...
gromer Posted September 22, 2007 Share Posted September 22, 2007 IMO, the easiest approach would be making a properties column. Eventually you will want one. propertyId, sellerId, buyerId, more columns that are needed for the property. Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353099 Share on other sites More sharing options...
mallen Posted September 23, 2007 Author Share Posted September 23, 2007 Thanks guys. While you both posted your replies I tried different combinations and somehow it worked! $query = "UPDATE phpbb_users SET title='{$_POST['title']}' , description='{$_POST ['description']}' , images='{$image_name}', address='{$_POST ['address']}', price='{$_POST ['price']}', contact='{$_POST ['contact']}', phone='{$_POST ['phone']}' WHERE user_id= '{$_SESSION['user_id']}'"; I will try your suggestions and play around more with it. Quote Link to comment https://forums.phpfreaks.com/topic/70280-insert-into-database-using-session-id/#findComment-353115 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.