Computing.Net > Forums > Web Development > PHP/MYSQL updating w/checkboxes

PHP/MYSQL updating w/checkboxes

Reply to Message Icon

Original Message
Name: jb60606
Date: July 1, 2007 at 19:47:24 Pacific
Subject: PHP/MYSQL updating w/checkboxes
OS: Linux
CPU/Ram: 2.8Ghz/512MB
Model/Manufacturer: Homeade
Comment:

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();
?>



Report Offensive Message For Removal


Response Number 1
Name: jb60606
Date: July 1, 2007 at 19:55:18 Pacific
Subject: PHP/MYSQL updating w/checkboxes
Reply: (edit)

btw, there are no errors or anything. It just doesn't update any records.


Report Offensive Follow Up For Removal

Response Number 2
Name: dmj2
Date: July 1, 2007 at 20:21:08 Pacific
Subject: PHP/MYSQL updating w/checkboxes
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 3
Name: jb60606
Date: July 1, 2007 at 21:02:16 Pacific
Subject: PHP/MYSQL updating w/checkboxes
Reply: (edit)

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".



Report Offensive Follow Up For Removal

Response Number 4
Name: jb60606
Date: July 1, 2007 at 21:07:01 Pacific
Subject: PHP/MYSQL updating w/checkboxes
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 5
Name: jb60606
Date: July 1, 2007 at 22:47:03 Pacific
Subject: PHP/MYSQL updating w/checkboxes
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP/MYSQL updating w/checkboxes

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge