Jump to content

insert into database using session id


mallen

Recommended Posts

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']}')";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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'] " ;

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Thanks guys. While you both posted your replies I tried different combinations and somehow it worked!  :D

 

$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.  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.