Jump to content

Archived

This topic is now archived and is closed to further replies.

yarub

[Solved] Update Multiple Rows At Once

Recommended Posts

Can someone tell me the process in updating multiple rows at once? I've been updating one row at a time with:
[code]$result = mysql_query("UPDATE tablename SET field='$field'")
or die(mysql_error());[/code]
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.

Share this post


Link to post
Share on other sites
try this
[code]
$result=@mysql_query("UPDATE table_name SET value1='$value', value2='$value2'",$cn) or die(mysql_error();
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. =\

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.