Jump to content

how to insert data into relational database


midgley

Recommended Posts

hi, im hoping somebody on this forum might be able to shed some light on how to insert data into a relational database

 

heres how i have my database set up:

https://cid-b405ca050a85fc76.skydrive.live.com/self.aspx/New%20folder/relationhips.jpg

 

so how would i go about inserting a new photo and linking it to an existing user?

 

this for instance doesnt work

INSERT INTO photos (photoName) VALUES ('dog') WHERE users.userId = '1'

 

can anybody help me out on this one?

 

 

Link to comment
Share on other sites

INSERT queries do not accept a WHERE clause, you either need to insert a new record....

 

INSERT INTO photos (photoName) VALUES ('dog');

 

or UPDATE an oexisting one....

 

UPDATE photos SET photoName = 'dog' WHERE id = 1;

Link to comment
Share on other sites

so i have to manually insert data into the USERSPHOTOS table then. like this:

-insert photoname into photos (which would auto increment photoId)

-then retrive the user id from users

-then insert the userid and photoid into the link table called USERSPHOTOS

 

can you let me know if this is the right way because i was thinking that mysql let you insert data into multiple tables all with one mysql_query thus letting mysql update the USERPHOTOS table for me.

Link to comment
Share on other sites

you spelled it out to me there, i was thinking that mysql was capable of cascading updates and thus saving me from typing out more querys. so as i understand it then, using mysql as a relational database is more about setting constraints than actually shortoning the code required to carry out a particular job. might look into this trigger thing but for now im going to keep things simple.

 

so mysql_insert_id will retreive the last id inserted into the database.

 

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.