Forum Moderators: coopster

Message Too Old, No Replies

Problem updating multiple rows of table

         

mattennant

6:46 pm on Dec 4, 2007 (gmt 0)

10+ Year Member



I've been working on this multiple row update page for what seems like forever

My problem is this, i'm trying to add the on submit code to the header, when i do the table does not update.

If i have the on submit code after the loop the table updates fine but i get the headers already send spiel, i'm sure it's one tiny thing that's causing the problem, but i'm really struggling to find it.

heres the code


$tbl_name = "display_tasks"; // Table name

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

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

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET task_deadline='$task_deadline[$i]', title='$title[$i]' WHERE number='$number[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:home.php");
}
mysql_close();

...................

//in the body

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><? $number[]=$rows['number'];?><? echo $rows['number'];?></td>
<td align="left"><? echo $rows['username'];?></td>
<td align="left"><input name="task_deadline[]" type="text" id="task_deadline" value="<? echo $rows['task_deadline'];?>"></td>
<td align="left"><input name="title[]" type="text" id="title" value="<? echo $rows['title'];?>"></td>
</tr>
<?php
}
?>
<?php
// table updates fine when the form submit code is here - but get headers already sent stuff
?>

thanks for looking
mat

cameraman

7:30 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't do this:
header("location:home.php");

if you've already sent anything at all to the browser, like this:
<tr>
<td align="left"><? $number[]=$rows['number'];?><? echo $rows['number'];?></td>
<td align="left"><? echo. . . . . .

You'll need to move the submit code above any possible output.

mattennant

9:43 am on Dec 5, 2007 (gmt 0)

10+ Year Member



cheers cameraman

problem is when i move the submit code up, the rows don't update. I can't for the life of me see why this should'nt work up in the header.

Any ideas

Kikikins

10:31 am on Dec 5, 2007 (gmt 0)

10+ Year Member



I can't spot any faults with what you have here. Maybe posting the rest of the page's code might help?

coopster

3:39 pm on Dec 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I can't for the life of me see why this should'nt work up in the header.

It's all in the logic. You cannot use the header() function if you have already "written" information out to the browser. Not even a blank space. So your logic may look something like this instead ...

if (submit was pressed) { 
// process my update
// close sql connection
// redirect user with the header()
}
/**
* otherwise,
* 1) SELECT your data
* 2) prepare your browser output
* 3) close sql connection
*/