Hey there i have some code i need to update multiple records at one time. can i get some help.

i have a page that selects what team i want to edit the stats for. then it sends it to another page where i can make changes to the stats. I can't figure this out at all. why is it not updating the players DB. i have another page where it is just changed approved field and that works fine but this is not. am i not doing something right. i will include the drop-down list page and the stats edit page as well as the page where i am just changing the approved field.

here is the link to the live pages so you can see how it works: http://bccsl.org/bccsltest/login/drop2.php

thanks in advance.

stefan

drop.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form id="teamsearch" name="teamsearch" method="post" action="update_multiple.php">
  <p>&nbsp;</p>
  <p>
    <label>
  <?php
  include 'dbc.php';
$query="SELECT teamname,teamid FROM teams";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=teamid value='$nt[teamid]'>Team Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  echo "<option value='$nt[teamid]'>$nt[teamname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

?>
<input name="submit" type="submit" id="submit" value="Submit" />
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>

</body>
</html>

update_multiple.php

<?php
include 'dbc.php';

$tbl_name="players"; // Table name 

// Connect to server and select databse.

$sql="select * from players where teamid='$_POST[teamid]'";
$result=mysql_query($sql);

// Count table rows 
// $count=mysql_num_rows($result);

// Count table rows
$count=mysql_num_rows($result);
echo 'count='.$count;

?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr> 
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Last name</strong></td>
<td align="center"><strong>Team ID</strong></td>
<td align="center"><strong>Games Played</strong></td>
<td align="center"><strong>Goals</strong></td>
<td align="center"><strong>Assists</strong></td>
<td align="center"><strong>Points</strong></td>
<td align="center" bgcolor="#FFFF00"><strong>Yellow Cards</strong></td>
<td align="center" bgcolor="#FF0000"><strong>Red Cards</strong></td>
<td align="center"><strong>Suspension</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['ID']; ?><? echo $rows['ID']; ?></td>
<td align="center"><input name="first[]" type="text" id="first" size="15" value="<? echo $rows['first']; ?>" disabled="disabled"></td>
<td align="center"><input name="last[]" type="text" id="last" size="15" value="<? echo $rows['last']; ?>" disabled="disabled"></td>
<td align="center"><input name="teamid[]" type="text" id="teamid" size="15" value="<? echo $rows['teamid']; ?>" disabled="disabled"></td>
<td align="center"><input name="gp[]" type="text" id="gp" size="10" value="<? echo $rows['gp']; ?>"></td>
<td align="center"><input name="goal[]" type="text" id="goal" size="10" value="<? echo $rows['goal']; ?>"></td>
<td align="center"><input name="assist[]" type="text" id="assist" size="10" value="<? echo $rows['assist']; ?>"></td>
<td align="center"><input name="point[]" type="text" id="point" size="10" value="<? echo $rows['point']; ?>"></td>
<td align="center"><input name="yellow[]" type="text" id="yellow" size="10" value="<? echo $rows['yellow']; ?>"></td>
<td align="center"><input name="red[]" type="text" id="red" size="10" value="<? echo $rows['red']; ?>"></td>
<td align="center"><input name="susp[]" type="text" id="susp" size="10" value="<? echo $rows['susp']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this 
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE `players` SET `gp`='{$gp[$i]}', `goal`='{$goal[$i]}', `assist`='{$assist[$i]}', `point`='{$point[$i]}', `yellow`='{$yellow[$i]}', `red`='{$red[$i]}', `susp`='{$susp[$i]}' WHERE `ID`='$ID[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:update_multiple.php");
}
?>

Players Table:

TABLE `players` (
  `ID` int(11) NOT NULL default '0',
  `first` varchar(255) default NULL,
  `last` varchar(255) default NULL,
  `address` varchar(255) default NULL,
  `city` varchar(255) default NULL,
  `postal` varchar(255) default NULL,
  `phone` varchar(255) default NULL,
  `feet` varchar(255) default NULL,
  `inch` varchar(255) default NULL,
  `weight` varchar(255) default NULL,
  `yyyy` varchar(255) default NULL,
  `mm` varchar(255) default NULL,
  `dd` varchar(255) default NULL,
  `teamid` varchar(255) default NULL,
  `status` varchar(255) default NULL,
  `gp` varchar(255) default NULL,
  `goal` varchar(255) default NULL,
  `assist` varchar(255) default NULL,
  `point` varchar(255) default NULL,
  `yellow` varchar(255) default NULL,
  `red` varchar(255) default NULL,
  `susp` varchar(255) default NULL,
  `approved` int(11) default '0',
  PRIMARY KEY  (`ID`)

Recommended Answers

All 8 Replies

Hey there i have some code i need to update multiple records at one time. can i get some help.

i have a page that selects what team i want to edit the stats for. then it sends it to another page where i can make changes to the stats. I can't figure this out at all. why is it not updating the players DB. i have another page where it is just changed approved field and that works fine but this is not. am i not doing something right. i will include the drop-down list page and the stats edit page as well as the page where i am just changing the approved field.

here is the link to the live pages so you can see how it works: http://bccsl.org/bccsltest/login/drop2.php

thanks in advance.

stefan

drop.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form id="teamsearch" name="teamsearch" method="post" action="update_multiple.php">
  <p>&nbsp;</p>
  <p>
    <label>
  <?php
  include 'dbc.php';
$query="SELECT teamname,teamid FROM teams";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=teamid value='$nt[teamid]'>Team Name</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  echo "<option value='$nt[teamid]'>$nt[teamname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

?>
<input name="submit" type="submit" id="submit" value="Submit" />
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>

</body>
</html>

update_multiple.php

<?php
include 'dbc.php';

$tbl_name="players"; // Table name 

// Connect to server and select databse.

$sql="select * from players where teamid='$_POST[teamid]'";
$result=mysql_query($sql);

// Count table rows 
// $count=mysql_num_rows($result);

// Count table rows
$count=mysql_num_rows($result);
echo 'count='.$count;

?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr> 
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Last name</strong></td>
<td align="center"><strong>Team ID</strong></td>
<td align="center"><strong>Games Played</strong></td>
<td align="center"><strong>Goals</strong></td>
<td align="center"><strong>Assists</strong></td>
<td align="center"><strong>Points</strong></td>
<td align="center" bgcolor="#FFFF00"><strong>Yellow Cards</strong></td>
<td align="center" bgcolor="#FF0000"><strong>Red Cards</strong></td>
<td align="center"><strong>Suspension</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['ID']; ?><? echo $rows['ID']; ?></td>
<td align="center"><input name="first[]" type="text" id="first" size="15" value="<? echo $rows['first']; ?>" disabled="disabled"></td>
<td align="center"><input name="last[]" type="text" id="last" size="15" value="<? echo $rows['last']; ?>" disabled="disabled"></td>
<td align="center"><input name="teamid[]" type="text" id="teamid" size="15" value="<? echo $rows['teamid']; ?>" disabled="disabled"></td>
<td align="center"><input name="gp[]" type="text" id="gp" size="10" value="<? echo $rows['gp']; ?>"></td>
<td align="center"><input name="goal[]" type="text" id="goal" size="10" value="<? echo $rows['goal']; ?>"></td>
<td align="center"><input name="assist[]" type="text" id="assist" size="10" value="<? echo $rows['assist']; ?>"></td>
<td align="center"><input name="point[]" type="text" id="point" size="10" value="<? echo $rows['point']; ?>"></td>
<td align="center"><input name="yellow[]" type="text" id="yellow" size="10" value="<? echo $rows['yellow']; ?>"></td>
<td align="center"><input name="red[]" type="text" id="red" size="10" value="<? echo $rows['red']; ?>"></td>
<td align="center"><input name="susp[]" type="text" id="susp" size="10" value="<? echo $rows['susp']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this 
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE `players` SET `gp`='{$gp[$i]}', `goal`='{$goal[$i]}', `assist`='{$assist[$i]}', `point`='{$point[$i]}', `yellow`='{$yellow[$i]}', `red`='{$red[$i]}', `susp`='{$susp[$i]}' WHERE `ID`='$ID[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:update_multiple.php");
}
?>

Players Table:

TABLE `players` (
  `ID` int(11) NOT NULL default '0',
  `first` varchar(255) default NULL,
  `last` varchar(255) default NULL,
  `address` varchar(255) default NULL,
  `city` varchar(255) default NULL,
  `postal` varchar(255) default NULL,
  `phone` varchar(255) default NULL,
  `feet` varchar(255) default NULL,
  `inch` varchar(255) default NULL,
  `weight` varchar(255) default NULL,
  `yyyy` varchar(255) default NULL,
  `mm` varchar(255) default NULL,
  `dd` varchar(255) default NULL,
  `teamid` varchar(255) default NULL,
  `status` varchar(255) default NULL,
  `gp` varchar(255) default NULL,
  `goal` varchar(255) default NULL,
  `assist` varchar(255) default NULL,
  `point` varchar(255) default NULL,
  `yellow` varchar(255) default NULL,
  `red` varchar(255) default NULL,
  `susp` varchar(255) default NULL,
  `approved` int(11) default '0',
  PRIMARY KEY  (`ID`)

it could be help to you.
in php code after POST the values....

for($i=0;$i<$count;$i++)
{
if($status[$i]==active)
{
$a=1;
}
else
{
$a=0;
}

$updated=mysql_query("update uregister set status='$a' where auto_id='$uid[$i]'");


}

i don't follow.... what would this do?

i don't follow.... what would this do?

here is the sample code.
follow this

<?php
ob_start();
extract($_REQUEST);
extract($_POST);
session_start();
if($_SESSION[userid]=='')
{
header('location:index.php');
}
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db=mysql_select_db('db',$con);
if($_POST)
{
$sel=mysql_query("select * from table");
$count=mysql_num_rows($sel);
//echo $count;exit;
for($i=0;$i<$count;$i++)
{
if($Goals[$i])
{
$a=$Goals[$i];
}


$updated=mysql_query("update uregister set status='$a' where auto_id='$uid[$i]'");


}

}


$select=mysql_query("select * from uregister");

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Flower Store Administration</title>
<style type="text/css">
<!--


</head>
<body>
<?php include "includes/header.php"; ?>
<?php include "includes/header1.php"; ?>


<table width="100%" height="450"  border="0" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <?php include "includes/leftpanel.php"; ?>
    <td width="1" bgcolor="#CCCCCC"><img src="images/paper.gif" width="1" height="1"></td>
	<td align="center" valign="middle">
	<table align="center" border="2">
	<form name="form" action="" method="post">
	<tr>
	<td align="center">userid</td>
	<td align="center">username</td>
	<td align="center">gender</td>
	<td align="center">address</td>
	<td align="center">email</td>
	<td align="center">phoneno</td>
	<td align="center">userstatus</td>
	</tr>
	<?
	
	while($fetch=mysql_fetch_array($select))
	{
	?>
	<tr align="center">
	<td align="center"><?=$fetch[auto_id]?></td>
	<td align="center"><?=$fetch[username]?></td>
	<td align="center"><?=$fetch[gender]?></td>
	<td align="center"><?=$fetch[address]?></td>
	<td align="center"><?=$fetch[email]?></td>
	<td align="center"><?=$fetch[phoneno]?></td>
	<input type="hidden" name="uid[]" value="<?=$fetch[auto_id]?>" />
	
	<td><input type="text" name="Goals[]" value="<?=$fetch[Goals]?>"></td>
	
	
	
	</tr>
	<?
	}
	?>
	<tr><td><input type="submit" name="update" value="update"></td></tr>
	</form>
	</table></td>
  </tr>
</table>

</body>
</html>

it is also an example same like as muralikalpana

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:update_multiple.php");
}
mysql_close();
?>

i did try the code above that posted by pushpakalpana..
It just show the value in table , and i unable to update "new value" into the table after clicked submit button..

anyone can help me to find out the problem ??

if(isset($_POST['Submit'])){
$x = array();
 for($i=0;$i<$count;$i++){
  $x[$i]['id'] = $_POST['id'][$i];
  $x[$i]['name'] = $_POST['name'][$i];
  $x[$i]['lastname'] = $_POST['lastname'][$i];
  $x[$i]['email'] = $_POST['email'][$i];
  $sql1="UPDATE $tbl_name SET name=$x[$i]['name'], lastname=$x[$i]['lastname'], email=$x[$i]['email'] WHERE id=$x[$i]['id']";
  }
}

Anyone can help me find the problem in this code....i still can't update the table after press "Submit" button....
Appreciate your help .........

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="1234"; // Mysql password
$db_name="new"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Password</strong></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><input name="id" type="text" id="id" value="<?php echo $rows['ID']; ?>"></td>
<td align="center"><input name="username[]" type="text" value="<?PHP echo $rows['USERNAME']; ?>"></td>
<td align="center"><input name="password[]" type="text"  value="<?PHP echo $rows['PASSWORD']; ?>"></td>

</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
$x = array();
 for($i=0;$i<$count;$i++){
  $x[$i]['id'] = $_POST['id'][$i];
  $x[$i]['username'] = $_POST['username'][$i];
  $x[$i]['password'] = $_POST['password'][$i];

  $sql1="UPDATE $tbl_name SET USERNAME=$x[$i]['username'], PASSWORD=$x[$i]['password'] WHERE ID=$x[$i]['id']";
  }
}$result1=mysql_query($sql1);


if($result1){
header("location:update_multiple.php");
}
mysql_close();
?>

//add this in action in form $_SERVER
<form name="form1" method="post" action="<? $_SERVER ?>">
//then
if(isset($_POST)){
$x = array();
for($i=0;$i<$count;$i++){
$x[$i] = $_POST[$i];
$x[$i] = $_POST[$i];
$x[$i] = $_POST[$i];
mysql_query("UPDATE $tbl_name SET USERNAME=$x[$i], PASSWORD=$x[$i] WHERE ID=$x[$i]");
if(!mysql_error()){
//header("location:update_multiple.php");
echo "UPDATE $tbl_name SET USERNAME=$x[$i], PASSWORD=$x[$i] WHERE ID=$x[$i]";
}
else{
echo mysql_error();
}
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.