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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP/MYSQL updating w/checkboxes

Reply to Message Icon

Name: jb60606
Date: July 1, 2007 at 19:47:24 Pacific
OS: Linux
CPU/Ram: 2.8Ghz/512MB
Product: 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();
?>




Sponsored Link
Ads by Google

Response Number 1
Name: jb60606
Date: July 1, 2007 at 19:55:18 Pacific
Reply:

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


0

Response Number 2
Name: dmj2
Date: July 1, 2007 at 20:21:08 Pacific
Reply:

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.


0

Response Number 3
Name: jb60606
Date: July 1, 2007 at 21:02:16 Pacific
Reply:

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



0

Response Number 4
Name: jb60606
Date: July 1, 2007 at 21:07:01 Pacific
Reply:

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.


0

Response Number 5
Name: jb60606
Date: July 1, 2007 at 22:47:03 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP/MYSQL updating w/checkboxes

php mysql www.computing.net/answers/webdevel/php-mysql/759.html

How do I setup PHP & MySQL on XP www.computing.net/answers/webdevel/how-do-i-setup-php-amp-mysql-on-xp/122.html

PHP, MySQL, phpMyAdmin www.computing.net/answers/webdevel/php-mysql-phpmyadmin/1590.html