By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

update check box

ddtpmyra
100+
P: 333
Hi Guru's
This is a continuation of my last post, but I decided to create a new thread for my new question.

Last post click here:

Problem:
How to update a check box value to mysql records

Code submitting the check box
[PHP]
# Print the top of a table or headers
echo "<table width='100%'><tr>";
echo "<td><b>Deadline Feedback</b></td>";
echo "<td><b>Check to approved</b></td>";
echo "<td><b>Date Created</b></td>";
echo "<td><b>Author</b></td>";
echo "<td><b>Requestor</b></td>";
echo "</tr>";

#Print data
while($row = mysql_fetch_assoc($result))
echo "<tr border=10><td>". $row['DeadLineFeedback']. "</td>";

#Here where I place the checkbox
echo "<td><input type=\"checkbox\" name=\"approved_$id\"></td>";
echo "<td>". $row['Created']. "</td>";
echo "<td>". $row['Author']. "</td>";
echo "<td>". $row['Requestor']. "</td>";
echo "</tr>";
[/PHP]

Code updating the check box
[PHP]

# assigning the checbox rows
$approved = $_POST['approved_$id'];

# This is where I'm having trouble how to tell my php code to pick the checked box

$query = "update fileStorage set approved='Y' where $approved=approved_$id";

# Execute the query
$result = mysql_query($query, $dbLink);

# Check if it was successfull
if($result)
{
echo "Success! Your file was updated!";
}
else
{
echo "Error! Failed to update";
echo "<pre>". mysql_error($dbLink) ."</pre>";
}

[/PHP]

Note:
Approved value = 'Y' or 'N' only
Sep 12 '08 #1
Share this Question
Share on Google+
6 Replies


P: 8
Hi ddtpmyra,

There are two steps to this solution:

1. Properly gathering the data

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <form action='checkboxread.php' method='post'>
  3. <input type='checkbox' name='field[]' value='1'   checked ='True'> Field 1<br>
  4. <input type='checkbox' name='field[]' value='2'> Field 2<br>
  5. <input type='checkbox' name='field[]' value='3'> Field 3<br>
  6. <input type='checkbox' name='field[]' value='4'> Field 4<br>
  7. <input type='checkbox' name='field[]' value='5'> Field 5<br>
  8. <input type='checkbox' name='field[]' value='6' checked ='True'> Field 6<br>
  9. <input type='hidden' name='fieldcount' value='6' >
  10. <input type='submit' value='Submit'>
  11. </form>
  12.  
  13. </html>
  14.  
2. Being able to retrieve the checkboxes values

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. echo "<html><head></head><body>";
  4.  
  5. // retrieve number of checkboxes to process
  6. $fieldcount = (int) $_POST['fieldcount'];
  7. echo "Number of checkboxes to process: $fieldcount </br>";
  8.  
  9. // set the array to N by default
  10. for ( $count = 1; $count <= $fieldcount; $count++ ) {
  11.    $field[$count] = "N";
  12. }
  13.  
  14. // retrieve the values that are ON, i.e. TRUE
  15. $checked = $_POST['field'];
  16.  
  17. // update the array with the TRUE
  18. if (sizeof($checked) > 0) {
  19.    foreach($checked as $item) {
  20.        $field[$item] = "Y";
  21.    }
  22. }
  23.  
  24.  
  25. // at this moment you have Y or Ns in your result array
  26. for ( $count = 1; $count <= $fieldcount; $count++ ) {
  27.    echo "field[" .$count . "] = $field[$count] </br>";
  28. }
  29.  
  30. echo "</body></html>";
  31.  
  32. ?>
  33.  

OUTPUT:

Number of checkboxes to process: 6
field[1] = Y
field[2] = N
field[3] = Y
field[4] = Y
field[5] = N
field[6] = N

So you should be able to retrieve specific values and update your table

Let me know how it goes,

Hope it helps,
phpNerd01
Sep 13 '08 #2

ddtpmyra
100+
P: 333
phpNerd01,

I can't have assigned field because I can't tell how many check box should I display I depends on the user how many files they will upload. That's why I have check boxes for each rows. And when they check the box I wanted it to be captured and update the records.

So there's only two options equivalent to my check box, Yes and No.

DM
Sep 15 '08 #3

ddtpmyra
100+
P: 333
anybody can help? I really need to finished this code now :(
Sep 16 '08 #4

pbmods
Expert 5K+
P: 5,821
How are you generating the inputs? Do you have a fixed number, or can the User add additional inputs using Javascript?
Sep 16 '08 #5

ddtpmyra
100+
P: 333
i am not knowledgeable on java.
but the script purpose is to display the each data (row) and im placing a check box to update a column name "approved" inside mysql.

this how it works:
the users will upload a file on the database after which the approving person will check the check box if it's approved or not. and here where I get confuse on how will i capture the row id that the approving person click (check) and for my script to execute the update query.

I hope I explain in very well, let me know if you have questions

thanks!
DM
Sep 17 '08 #6

Atli
Expert 5K+
P: 5,058
i am not knowledgeable on java.
Knowing that Java is not JavaScript would be a good start ;)

But, as to you problem.

All you would have to do to print the boxes would be to fetch them from your database and echo an <input> element for each of them.
Somewhat like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $result = mysqli_query("SELECT `boxID` FROM `boxTable` WHERE `whatever`");
  3. while($row = mysqli_fetch_assoc($result)) 
  4. {
  5.     echo '<br /><input type="checkbox" name="boxList[]" value="'. $row['boxID'] .'" />'. $row['boxID'];
  6. }
  7. ?>
  8.  
Then, once that is submitted, all the boxes that were checked would be sent as an array named 'boxList' into the $_POST super-global, which you could use to update your database.

You could use the IN clause to update them all at once.
Like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['boxList'])) 
  3. {
  4.     $inString = implode(", ", $_POST['boxList']);
  5.     $sql = "UPDATE boxTable SET `approved` = TRUE WHERE `boxID` IN($inString)";
  6.  
  7.     // etc...
  8. }
  9. ?>
  10.  
This will obviously have to be adapted to fit you data and you other code, but it's the general idea.
Sep 17 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.