954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Multiple row edit

Hello,

I have run into a problem where I need to update multiple rows in an SQL table at once. I have found and tried to use the tutorials on setting an ID and then looping through the data that way ( http://www.daniweb.com/forums/thread35096.html ) but my setup is a little different:

The table would look something like this:

ID | ItemName | Owner
0 | test | me
1 | test2 | you
2 | tester | me

Note how the ID number does NOT go in a sequence with the owner.

I need to ONLY edit data based on the owner (the owner being who is logged in).

I haven't been able to make that happen, if someone could help me out it would be appreciated.

~Turt2Live

turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
 

> Note how the ID number does NOT go in a sequence with the owner.

OK, as a rule your owner field would be an integer linked to a 'users' table and the itemname would also be an integer linked to an 'items' table. So your resulting table would be what's commonly called a 'link table'.

users
user_id | name | (other fields pertaining to the user, e.g. password, email etc)

items
item_id | itemname | (other fields pertaining to the item)

useritems (the link table)
item_id | user_id

Your logged in user (usually identified by a session variable, e.g. user_id: $_SESSION['user_id']) can then view their items thus:

$user_id =  $_SESSION['user_id'];
$rs = mysql_query("SELECT i.item_id, i.itemname FROM items AS i ON useritems AS ui INNER JOIN i.item_id = ui.item_id WHERE ui.user_id = $user_id");


I've used aliases (AS) above - they cut down on having to name the full table as a prefix every time.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: