Forum Moderators: coopster

Message Too Old, No Replies

Multiple records update ( help need )

Mysql and php

         

billbody

4:34 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi I repost and old post, may be someone can help me.

I had set up a table like this.

Id
Name
Win
Draw
Lost
Points

I need a page from where I can update all records at the same time. I can update one by one, can show the data, etc etc, but have no idea on how to update or delete multiple records via one single file.

T.I.A

jatar_k

4:40 pm on Jul 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld billbody,

as coopster asked originally, what exactly are you having trouble with?
What have you tried so far?
Do you want to update each row with a different value or put the same value in each row?
updating all rows can be very messy

Multiple deletes are easy

delete from tablename where id in ();

between the parentheses you put the list of ids seperated by a comma

billbody

5:01 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Hi and thanks to your fast reply.
Well i will try to be a more specific.

The idea is to have and "ranking" position of about 15 teams of a legue.
So I need to update once a week all the results.

So $id $name will be always the same.
Win, lost, draw will be update for each team.

something like this:
-----------------------------------------------------
Id ¦ $name ¦win ¦ draw ¦ lost ¦ points
1 team1 1 0 0 5
2 team2 0 1 0 1
3 team3 0 0 1 0

So I can update the fields of all the teams in one single page and then click update and....PRESTO!

As I said before I have no problem, doing this one by one, and even making a page to show all this data as a form, but can't make the UPDATE thing works

any idea?

jatar_k

5:10 pm on Jul 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could make a form that shows all 15 teams, use a loop to build it, make all the form element names the same except use the id at the end of each, maybe with a specific character positioned between the name and the id so you can split it to get the id to use in your update query.

When it is posted the array can be looped through and each update query can be issued.

should work

billbody

5:24 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



well the only problem is...that I have no idea on how to make the loop , etc etc
Can I make and example?

I usually use DW to make make pages, yes I kown..it's not the best way, and the cleanest code, but it works.

T.I.A

jatar_k

6:30 pm on Jul 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



DW

well that explains it, hehe ;)

ok, seriously, try this for an example

you could create your form something like so though you would have to select data first, I just faked an array

<?
// you should populate this from your ids selected from your db
$idarr = array(1,5,7,24,55,76);
?>
<form method="post" action="loopupdate.php">
<?
$counter = 0;
foreach ($idarr as $nextid) {
echo '<p>name: <input type="text" name="name',$counter,"\">\n";
echo '<br>wins: <input type="text" name="win',$counter,"\">\n";
echo '<br>draws: <input type="text" name="draw',$counter,"\">\n";
echo '<br>losses: <input type="text" name="lost',$counter,"\">\n";
echo '<br>points: <input type="text" name="points',$counter,"\">\n"
echo "<input type=\"hidden\" name=\"id",$counter,"\" value=\"",$nextid,"\">\n\n";
$counter++;
}
?>
<p><input type="submit" value="update them all">
</form>

then create a file called loopupdate.php and put it in the same directory

<?
$counter = 0;
while (isset($_POST['name' . $counter])) {
echo "<p>update tablename set name='" . $_POST['name' . $counter] . "', win='" . $_POST['win' . $counter] . "', draw='" . $_POST['draw' . $counter] . "', lost='" . $_POST['lost' . $counter] . "', points='" . $_POST['points' . $counter] . "' where id=",$_POST['id' . $counter] . "\n";
$counter++;
}
?>

that's about it

billbody

7:50 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



ok, so I must include a querry first to get the data from the table and then put your code starting from the array?

jatar_k

7:51 pm on Jul 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try it as is first to get an understanding of what it is doing.

then start making changes from there

billbody

2:00 am on Jul 17, 2005 (gmt 0)

10+ Year Member



ok well I think I kind of lost again :-(

I did as you told me, but it brings up a page with X number of fields to update but with no data preloaded

How do I get the actual data to be displayed and then update it?

Sorry for my english, and thanks again