I have a form which prints out a number of rows and gives the user the option to activate/deactive and add comments to whatever items they wish. The user should then press "Submit" and have the form redisplay with the updated entries.
It seems to update the database correctly, but the page refreshes (or resets?) without showing the changes. If I hit submit again, the changes appear, submit again, changes disappear. I'm using a multiple row form for the first time as well as submitting to the same page, so not sure if it's refresh issue or an array issue.
One example uses header("Location: pubmed.php"); after the sql update statement - which gives me the "headers already sent" error. This is usually a spacing issue right? Why would it work in the middle of someone's code at all, then?
If I substitute a meta refresh tag or nothing, the form seems to reset rather than display the correct results.
Any thoughts?
<?php
echo "<form action='' method='post' name='pubs'>";
$query="SELECT * FROM pubmed ORDER BY pubmed.text ASC LIMIT 10";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<h2>Pubmed Queries (" . $num_results. ")</h2>";
echo "<table class='data'>";
while ($row = mysql_fetch_array($result)){
if ($row['activated']=='yes') {
$color_bg='trans'; $active = "Active";
} else {$color_bg='bgLight'; $active = " X-(_ ";}
echo "<tr class='" . $color_bg . "'>";
echo "<td>";
echo "<input name='id[]' type='hidden' value='". $row['id'] ."'>";
echo "<b>UID: ". $row['id'] . "</b><br>";
echo $active;
echo "</td>";
echo "<td>";
echo "<select name='activated[]' size='1'>";
echo "<option value='yes'";
if ($row['activated'] == 'yes'){echo " selected";}
echo ">active</option>";
echo "<option value='no'";
if ($row['activated'] == 'no'){echo " selected";}
echo ">inactive</option>";
echo "</select>";
//echo "<input name='activated[]' type='checkbox' value='" . $row['activated'] . "' ". $checkbox . ">";
//if ($checkbox) {$row['activated']='no';
echo "</td>";
echo "<td>";
echo stripslashes($row['authors']);
echo "</td>";
echo "</tr>";
echo "<tr class='" . $color_bg . "'>";
echo "<td>";
echo stripslashes($row['link']);
echo "</td>";
echo "<td>";
echo stripslashes($row['img']);
echo "</td>";
echo "<td>";
echo stripslashes($row['text']) . "<br>";
echo "comments: <input name='comments[]' type='text' value='" . $row['comments'] . "' size='60' maxlength='255'>";
echo "</td>";
echo "</tr>";
echo "<tr><td colspan='3'><hr></td></tr>";
}
echo "</table>";
echo "<input name='Submit' type='submit' value='submit'> <input name='reset' type='reset' value='Reset'></form>";
//Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$num_results;$i++){
$update_query=mysql_query("UPDATE pubmed SET comments='$comments[$i]', activated='$activated[$i]' WHERE id='$id[$i]'");
}
if($update_query){
header("Location: pubmed.php");
// headers already sent error
}
else {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
mysql_close();
}
?>