Forum Moderators: coopster

Message Too Old, No Replies

update multiple field(s) in multiple records in one form, one time

update multiple field(s) in multiple records in one form, one time

         

mzeed

9:12 am on Jan 21, 2008 (gmt 0)

10+ Year Member



greeting

i have a mysql database and i use php to query all the rows in the database. i bring the results into an html table like so:

¦------------¦------------¦------------¦
¦ Team_Name ¦Team_Points ¦ Team_Rank ¦
¦------------¦------------¦------------¦
¦ Team one ¦ 20 ¦ 1 ¦
¦ Team tow ¦ 18 ¦ 2 ¦
¦ Team three ¦ 15 ¦ 3 ¦
¦ Team four ¦ 10 ¦ 4 ¦
¦------------¦------------¦------------¦

the (Team_Points & Team_Rank) are editable fields.
i want change the (points & rank) value for 2 or 3 teams at the same time and save all the changes.

how to do that?

Regards

venelin13

9:48 am on Jan 21, 2008 (gmt 0)

10+ Year Member



You should set the input fields names as arrays:

<input type="text" name="team_points[]" value="20" />
<input type="text" name="team_score[]" value="1" />

You should have some identifyer for each row, for example, the team id number. Each of the table rows may looks like:

<tr>
<td>$team_name <input type="hidden" name="id[]" value="1" /></td>
<td><input type="text" name="team_points[]" value="20" /></td>
<td><input type="text" name="team_rank[]" value="1" /></td>
</tr>

After the form is been submited:

foreach($_POST['id'] as $key=>$team_id){
$team_points = $_POST['team_points'][$key];
$team_score = $_POST['team_score'][$key];

$sql = "update my_table set team_points = '$team_points', team_score = '$team_score' where team_id = '$team_id' ";

}

mzeed

10:56 am on Jan 21, 2008 (gmt 0)

10+ Year Member



thank you dear venelin13,

i'll try it today.

henry0

12:04 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another way using check box

Example
*/
echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";
echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";
echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";
echo "<input type=checkbox name=cbox[] value=".$row['item_id'].">";

//on the action page
//sizeof() or count() same job

if(count($_POST['cbox'])) {

//means if at least one check box is selected

foreach($_POST['cbox'] AS $id) {

mysql_query("UPDATE tablename SET ... WHERE item_id=$id");
} //end foreach

} //end IF

mzeed

12:37 am on Jan 23, 2008 (gmt 0)

10+ Year Member



I'm Back again.
it is not working :(

it's give wrong:
Warning: Invalid argument supplied for foreach() ....table.php on line 102

I don't know what the problem :(

henry0

12:37 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post your code pertaining to the section that gives the error.

mzeed

5:53 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



ok, this is my code:

if ($act=="EditTable") {
$id = $_POST['id'];
foreach($id as $key => $ids){
$points = $_POST['points'][$key];
$rank = $_POST['rank'][$key];

$sql = "update team_table set points = '$points', rank = '$rank' where id = '$ids' ";

}

henry0

6:21 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you sure that $_POST isset and !empty

mzeed

9:30 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



-----------------
Are you sure that $_POST isset and!empty
-----------------

:( it's empty!

Help plz.

henry0

10:05 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post more script :)

we need to know how you deal with POST values
(where the POSTs comes from, the form etc.)

mzeed

6:22 am on Jan 24, 2008 (gmt 0)

10+ Year Member



this is my form:


if ($go=="edittable") {
$pilih=mysql_query("select * from team_table");
$jumlah=mysql_num_rows($pilih);
echo "&nbsp;<center>Curently we have $jumlah Clubs</center>.
<p></p>
<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bgcolor=\"#9D9D00\" align=\"center\">
<tr>
<td>
<form action=\"?go=update&act=EditTable\" method=\"post\" name=\"catformedittable\">
<table width=\"500\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\" align=\"center\" bgcolor=\"#E7E7CF\">
<tr align=\"center\" bgcolor=\"#b6C68C\">
<!--<td><B>ID</B></td>-->
<td><b>Rank</b></td>
<td width=\"300\"><b>Team</b></td>
<td><b>Points</b></td>
</tr>
";
$query = "SELECT * FROM team_table ORDER BY rank";
$qry = @mysql_query($query,$connect) or die ("Query salah");
while ($row = mysql_fetch_array ($qry)) {
echo "<tr bgcolor=\"#C6C68C\">";
echo "<input name=\"id[id]\" value=$row["id"] size=2 class=textbox type=hidden>";
echo "<td align=center><input name=rank[] value=$row["rank"] size=2 class=textbox>";
echo "</td><td>$row["team"]</td>
<td align=\"center\">
<input name=points[] value=$row["points"] size=2 class=textbox></td>
</tr>";
}
echo "<tr><td align=center colspan=\"3\">
<input type=\"submit\" name=\"submit\" value=\"Edit\" class=\"boxlook\"> &nbsp;
<input type=\"reset\" value=\"Reset\" class=\"boxlook\">
</td></tr></table>
</form>
</td></tr></table>";
}

and thank you for help henry0 :)

henry0

12:35 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This has been tested and works :)

your problems:
Never use @ in a query while in dev mode
use mysql error while in dev mode
you had wrong quotes and double quotes
and missing quotes where expected
Have fun!

<?php
function db_connect()
{
$result = mysql_pconnect("localhost", "root", "");
if (!isset($result) && empty($result))
{echo "can't connect!"; }
if (!@mysql_select_db("your_table"))
return false;
return $result;
}
$connect=db_connect();

$query = "SELECT * FROM team_table ORDER BY rank";
$qry = mysql_query($query,$connect) or die (mysql_error());
while ($row = mysql_fetch_array ($qry)) {
echo "<tr bgcolor=\"#C6C68C\">";
echo "<input name='id[id]' value=$row[id] size='2' class='textbox' type='hidden'>";
echo "<td align=center><input name=rank[] value=$row[rank] size='2' class='textbox'>";
echo "</td><td>$row[team]</td>
<td align=\"center\">
<input name=points[] value=$row[points] size='2' class='textbox'></td>
</tr>";
}
?>

<edit> if you use my conn() don't forget adding your password, none was used for I tested on my local office machine</edit>

mzeed

2:35 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



thank you for advices and help man :)

but, it's still show the same error :(

henry0

7:41 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I know where it comes from
you have an error because (of course) no value for ID (from POST) exists yet
I tried it, but as soon as submitted the error is gone

separate the foreach from that page and bring it to the landing page.

phnord

9:29 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



I think if you separate out your logic (code) from your presentation things will be much cleaner and you can debug your code better and figure out why things aren't working. For instance you hardcode all your SQL statements right into the page. That's going to be a nightmare down the road if you ever need to update an SQL statement and you've meshed thousands of lines of code with the presentation...or if another developer takes over and needs to make changes to your code.