Jump to content

Can I update multiple rows in table at once from data displayed in a table?


bajangerry

Recommended Posts

Hi Guys,

I need some help here understanding how best to update multiple rows in a table at once. I have a table which displays a number of columns, one of which I need to have the users enter some data in and when they have completed all the rows then they should click a button and the database table should be updated with the new data. I am stumped as to how to achieve this multiple row update at once. The code I have so far simple displays the data and I can not figure out where to go from here:

Any help gratefully accepted!

 

<?php

include "lib/config.php";

top();

$no=$_GET['no'];

?>

 

<html>

<head>

<title>Mobile Calls</title>

<style type="text/css">

 

</style>

</head>

<body>

<br>

<div align="center"> <font size = "3" color="#FF0000"><strong>Calls made by Mobile Number: <?echo ($no)?> </strong></font></div><br>

<table id="printTable" border="1" id="mobilenumbers">

  <tr>

    <td nowrap><div align="center"> <font size="3"><strong>Date</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Location</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Called No</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>System Time</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Call Length</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Type</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Call Cost</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Account Code</strong></font></div></td>

  </tr>

<?

$result = mysql_query("SELECT * FROM mobile WHERE `service` = '$no'");

while ($row = mysql_fetch_object($result)){

 

?>

    <td><div align="center"><font size="3"><? echo($row->date);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->location);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->dialed);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->time);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->duration);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->type); ?></font></div></td>

    <td><div align="center"><font size="3"><?php echo "$"?><?php echo number_format($row->cost, 2, '.', '.');?></font></div></td>

    <td><div align="center"><input name="accode" type="text" id="accode" size="6"value=<?php echo ($row->accountcode);?>></font></div></td>

  </tr>

<?

}

?>

</table>

</body>

</html>

put a count in your while and name each textfield accode$count. also add a hidden with some kind of identifier for that row. name='hidden$count' and value='$mobile_id'.

 

then when you submit cycle through the post array. check for any accode fields and update according to the hidden field id.

schilly,

I think I need some further help with this as I am having trouble figuring out how to "cycle" through the post array, can you explain what you mean by that? I have got this working if I have one entry in the table and I can update the field fine that way but once there are more than one row in teh table I am having a problem.

schilly,

I think I need some further help with this as I am having trouble figuring out how to "cycle" through the post array, can you explain what you mean by that? I have got this working if I have one entry in the table and I can update the field fine that way but once there are more than one row in teh table I am having a problem.

 

If you output this as html code:

<input type="text" name="location[12]">

 

Browser will send output as array. You can use foreach or something to iterate trough it and execute mass update query.

 

Goat

 

Goat,

My problem here is ensuring that the update is applied to the correct row in the table which does NOT have a unique identifier(stupid I know, but I inherited it this way). So I am hoping to send multiple fields so that the update query will be able to use them to confirm that the row is the correct one to update. That being the case I am having a problem understanding how I can get these multiple fields back out of the array with the FOREACH to be able to use them in the update query. This is what I have so far now with the update query commented out and the foreach results echoed to screen.

 

<?php

include "lib/config.php";

top();

$no=$_GET['no'];

 

if(isset($_POST['send']))

{

foreach($_POST as $key=>$value) {

echo $key.": ". $value;

echo "<br>";

 

//$update = "UPDATE mobile SET accountcode = '$code' WHERE dialed = '$dialed' AND location = '$location' AND date = '$date' AND time = '$time' AND duration = '$duration'";

//$result = mysql_query($update) or die(mysql_error());

//flush();

}

}

?>

 

<html>

<head>

<title>Mobile Calls</title>

<style type="text/css">

 

</style>

</head>

<html>

<body>

<br>

<div align="center"> <font size = "3" color="#FF0000"><strong>Calls made by Mobile Number: <?echo ($no)?> </strong></font></div><br>

<form name="test" method="post">

<table id="printTable" border="1" id="mobilenumbers">

  <tr>

    <td nowrap><div align="center"> <font size="3"><strong>Date</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Location</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Called No</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>System Time</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Call Length</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Type</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Call Cost</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Account Code</strong></font></div></td>

  </tr>

<?

$result = mysql_query("SELECT * FROM mobile WHERE `service` = '$no'");

$count += 0;

while ($row = mysql_fetch_object($result)){

$count += 1;

?>

    <td><div align="center"><font size="3"><? echo($row->date);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->location);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->dialed);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->time);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->duration);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->type); ?></font></div></td>

    <td><div align="center"><font size="3"><?php echo "$"?><?php echo number_format($row->cost, 2, '.', '.');?></font></div></td>

    <td><div align="center"><input name="accode<? echo $count;?>" type="text" id="accode" size="6"value=<?php echo ($row->accountcode);?>></font></div></td>

    <input type="hidden" name="count<? echo $count;?>" value="<?php echo $count?>">

    <input type="hidden" name="date<? echo $count;?>" value="<?php echo ($row->date)?>">

    <input type="hidden" name="location<? echo $count;?>" value="<?php echo ($row->location)?>">

    <input type="hidden" name="dialed<? echo $count;?>" value="<?php echo ($row->dialed)?>">

    <input type="hidden" name="time<? echo $count;?>" value="<?php echo ($row->time)?>">

    <input type="hidden" name="duration<? echo $count;?>" value="<?php echo ($row->duration)?>">

  </tr>

<?

}

 

?>

</table>

<br>

<input name="send" type="submit" value="Update">

</form>

</body>

</html>

You say you don't have primary key in your table? Really strange, I am not mysql expert, but I doubt that you can even create table with no primary key.

 

however if you type something like:

 

<input name="accode[<? echo $count;?>]" type="text" size="6"value=<?php echo ($row->accountcode);?>>

 

browser WILL output it as array. You can use same thing for all hidden fields. Then you can use something like this for iteration:

 


for($counter = 0; $counter < sizeof($_POST['accode']); $counter++)
{
  $sql = "UPDATE table SET accode='".$_POST['accode'][$counter]."' WHERE date='".$_POST['date'][$counter]."' AND " ///......
  mysql_query($query);
}

 

 

Goat

Goat,

I added a primary key to the table (mysql will allow you to create table with no indexes although why you would do so I do not know) as a column called "id" which will auto increment. I still am not getting the results I expect though, even when I echo the count value I get nothing showing up:

 

<?php

include "lib/config.php";

top();

$no=$_GET['no'];

 

if(isset($_POST['send']))

{

for($counter = 0; $counter < sizeof($_POST['accode']); $counter++)

{

echo "size of array = ".sizeof($_POST['accode'])."<br>";

$update = "UPDATE mobile SET accountcode='".$_POST['accode'][$counter]."' WHERE id ='".$_POST['id'][$counter]."' ";

$result = mysql_query($update) or die(mysql_error());

//flush();

}

}

?>

 

<html>

<head>

<title>Mobile Calls</title>

<style type="text/css">

 

</style>

</head>

<html>

<body>

<br>

<div align="center"> <font size = "3" color="#FF0000"><strong>Calls made by Mobile Number: <?echo ($no)?> </strong></font></div><br>

<form name="test" method="post">

<table id="printTable" border="1" id="mobilenumbers">

  <tr>

    <td nowrap><div align="center"> <font size="3"><strong>Date</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Location</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Called No</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>System Time</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Call Length</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Type</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Call Cost</strong></font></div></td>

    <td nowrap><div align="center"> <font size="3"><strong>Account Code</strong></font></div></td>

  </tr>

<?

$result = mysql_query("SELECT * FROM mobile WHERE `service` = '$no'");

$count += 0;

while ($row = mysql_fetch_object($result)){

$count += 1;

?>

    <td><div align="center"><font size="3"><? echo($row->date);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->location);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->dialed);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->time);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->duration);?></font></div></td>

    <td><div align="center"><font size="3"><?php echo ($row->type); ?></font></div></td>

    <td><div align="center"><font size="3"><?php echo "$"?><?php echo number_format($row->cost, 2, '.', '.');?></font></div></td>

    <td><div align="center"><input name="accode<? echo $count;?>" type="text" id="accode" size="6"value=<?php echo ($row->accountcode);?>></font></div></td>

    <input type="hidden" name="id" value="<?php echo $row->id?>">

  </tr>

<?

}

 

?>

</table>

<br>

<input name="send" type="submit" value="Update">

</form>

</body>

</html>

Sorry goat but I am not totally confused by whatyou mean in your last post :confused

 

"Add [] in accode input name"... where exactly do you mean?

 

I got the output printing with the print_r($_POST); and I can see data is being passed but still not getting it into the table...  : :o:-[

 

Ok, did that so that the output from my " print_r($_POST);" give me:

 

Array ( [accode] => Array ( [1] => 99999 [2] => 12345 ) [id] => Array ( [1] => 1 [2] => 2 ) [send] => Update )

 

which actually updates the first row in the table but not the second for some reason. I have the following:

 

if(isset($_POST['send']))

{

for($counter = 0; $counter < sizeof($_POST['accode']); $counter++)

{

$update = "UPDATE mobile SET accountcode='".$_POST['accode'][$counter]."' WHERE id ='".$_POST['id'][$counter]."' ";

$result = mysql_query($update) or die(mysql_error());

//flush();

}

}

 

Any suggestion as to why the second row does not get updated?

My fault. The counter should start from one , and it should go to sizeof.

 

for($counter = 1; $counter <= sizeof($_POST['accode']); $counter++)

 

either that or change other part of code, so id and other array start from 0.

 

Goat

Archived

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

×
×
  • Create New...

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.