Forum Moderators: coopster
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
<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' ";
}
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
if ($go=="edittable") {
$pilih=mysql_query("select * from team_table");
$jumlah=mysql_num_rows($pilih);
echo " <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\">
<input type=\"reset\" value=\"Reset\" class=\"boxlook\">
</td></tr></table>
</form>
</td></tr></table>";
}
and thank you for help henry0 :)
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>