Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I got a little help on this at PHPFreaks, though I still can't get it to take. The following code basically extracts data from a database, and prints it to a webform where the user can edit/update the records of their choosing.
Displaying the data in the form works fine, though updating does absolutely nothing.
Can anyone spot anything wrong with the code? The DB update code is towards the bottom.
Thanks in advance.
<?php
include '../../includes/db.php';$query = "SELECT DISTINCT sym, id, e_date, e_price FROM symbols ORDER by sym";
$result = mysql_query($query);
$count = mysql_num_rows($result);while($row = mysql_fetch_array($result)) {
?><tr>
<td><div align="center"><input name="checkbox[<?php echo $row['id'];?>]" id="checkbox[<?php echo $row['id'];?>]" type="checkbox" value="<? echo $row['id']; ?>"></div></td>
<td><input type="text" name="new_sym[<? echo $row['id'];?>]" id="new_sym" value="<? echo $row['sym']; ?>"></td>
<td><input type="text" name="new_e_date[<? echo $row['id'];?>]" id="new_e_date" value="<? echo $row['e_date']; ?>"></td>
<td><input type="text" name="new_e_price[<? echo $row['id'];?>]" id="new_e_price" value="<? echo $row['e_price']; ?>"></td>
<td>View/Edit Notes on this Record</td>
<td> </td>
</tr><?php
}
if($_POST['update'])
{$new_sym=$_POST['new_sym'];
$new_e_date=$_POST['new_e_date'];
$new_e_price=$_POST['new_e_price'];
$checked = $_POST['checked'];
foreach($checked as $key => $value){
$id= $value;
$sql = "UPDATE symbols SET sym='$new_sym[$id]', e_date='$new_e_date[$id]', e_price='$new_e_price[$id]' WHERE id='$id'";
$result = mysql_query($sql) or die(mysql_error()."<br />SQL: $sql");
}//if successful redirect to records.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=records.php\">";
}
}
mysql_close();
?>

So does that section of code actually run?
I would put an echo $sql after ur mysql_query, try & paste the exact query into the command line & see what happens.
And, $_POST[update] exists right?? Maybe not, $_GET[update] ?? I've pulled my hair out before when getting that one wrong.

Yeah $update exists. It's the form submit button - I just
left it out of my first post.I echoed the $sql (and $result) and it returned nothing.
However, I also disabled the auto-redirect/refresh at the
bottom of the code and found this error:"Warning: Invalid argument supplied for foreach() in
(filename) on line 415".

the $checked variable makes sense to me, but I don't know
where it's getting it values from if it's not included in the
HTML.

problem solved!
In the HTML, "checkbox[]" is posted to the script. In the PHP
code, "$_POST['checked'];" is what was expected. I thought
they were two different things before but it hit me when I
was writing out that reply to you.Thanks

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |