Jump to content


Photo

[Solved] Update Multiple Rows At Once


  • Please log in to reply
7 replies to this topic

#1 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 21 October 2006 - 08:09 AM

Can someone tell me the process in updating multiple rows at once? I've been updating one row at a time with:
$result = mysql_query("UPDATE tablename SET field='$field'") 
or die(mysql_error());
I'm curious as to how I could have multiple rows at once, and then edit them all all at one time. Let's use a user database as an example. Right now, I just print out every single user with a link as their name which takes me to their own page. I'd like to be able to edit the page in which they all print out.

Username: [ yarub ] Password: [ **** ] Email: [ yarub@yarub.com ]
Username: [ fred ] Password: [ **** ] Email: [ fred@yarub.com ]
Username: [ john ] Password: [ **** ] Email: [ john@yarub.com ]
[ update ]

The brackets represent input fields obviously. Can someone run me through the process of making this work? I have absolutely no idea and can't find any tutorials on it.

Thanks.

EDIT: Solved! Got some help from someone. Thanks to those who attempted to help me.

#2 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 22 October 2006 - 04:45 AM

Any takers?

#3 fert

fert
  • Members
  • PipPipPip
  • Advanced Member
  • 1,114 posts

Posted 22 October 2006 - 06:39 AM

try this
$result=@mysql_query("UPDATE table_name SET value1='$value', value2='$value2'",$cn) or die(mysql_error();


Si hoc legere scis nimium eruditionis habes
Gentoo Linux 2007.0 Firefox 2

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 October 2006 - 08:34 PM

If you mean update different rows that don't match a single condition with a single statement, then the answer is no.  You'd have to issue a separate update statement for each UID.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 23 October 2006 - 04:00 AM

Well, I have absolutely no idea where to start. For the database, I'll just say I have four fields: id, user, password, and email. The id is set to primary and auto inc. I'm looking for a way to update every single row by clicking the update button at the bottom.

I know that it's possible to do that because I've seen it on other scripts. I'm just looking for something simple and something I can understand. My friend said it might involve a "loop" or a "foreach" function... but I don't know anything about either. =\

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 October 2006 - 02:32 AM

Well, it really depends on how you've declared each of the fields in the HTML form.  Did you use a common prefix for all related fields for each record?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 yarub

yarub
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts
  • LocationSioux City, IA

Posted 24 October 2006 - 04:06 AM

I'm not sure what you mean. Right now, just to get it working, I have the following fields: id, name, lastname, & email. The field "id" is primary and atuo inc. I'm not sure what you mean by prefix.

I literally have no idea where to even start with this. I don't know what I need for the field name. I don't know what I need for the actual function to make it update. I'm working with scratch here because I've never done it.

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 October 2006 - 03:33 PM

I was talking about the HTML output side first... but it sounds like you've found someone else who can get you going.  Good luck.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users