473,388 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to update data in table using form and a checkbox?

I have a php script that shows all the data in a table. I want it to have check boxes next to each line of this data that it puts out, so I can check the boxes next to certain entries, and when I click submit, the "have" collumn for those entries is set to yes.

Here is most of the script that shows the list:

Expand|Select|Wrap|Line Numbers
  1. $query="SELECT * FROM get ORDER BY artist,name ASC";
  2. $result=mysql_query($query);
  3.  
  4. $num=mysql_numrows($result);
  5.  
  6. mysql_close();
  7. ?>
  8. <table border="0" cellspacing="2" cellpadding="2">
  9. <tr>
  10. <th><font face="Arial, Helvetica, sans-serif">Name</font></th>
  11. <th><font face="Arial, Helvetica, sans-serif">Artist</font></th>
  12. <th><font face="Arial, Helvetica, sans-serif">Album</font></th>
  13. <th><font face="Arial, Helvetica, sans-serif">Have</font></th>
  14. </tr>
  15.  
  16. <?php
  17. $i=0;
  18. while ($i < $num) {
  19.  
  20. $f1=mysql_result($result,$i,"name");
  21. $f2=mysql_result($result,$i,"artist");
  22. $f3=mysql_result($result,$i,"album");
  23. $f4=mysql_result($result,$i,"have");
  24. ?>
  25.  
  26. <tr>
  27. <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
  28. <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
  29. <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
  30. <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
  31. </tr>
  32.  
  33. <?php
  34. $i++;
  35. }
  36. ?>
  37. </body>
  38. </html>
To try to help out whoever helps(becuase I honestly have no idea how to do this) The MySQL query to put Yes in the Have column would be similar to this:

Expand|Select|Wrap|Line Numbers
  1. UPDATE get SET have = YES WHERE name = $CheckedRows
Jan 24 '11 #1

✓ answered by Markus

Hi, flametail.

Firstly you need to associate some value with your checkboxes so that you can read this data when your form is submit. At this point I will assume that your rows in your database have some unique ID associated with them. So your checkboxes will look something like:

Expand|Select|Wrap|Line Numbers
  1. <input 
  2.   type="checkbox" 
  3.   name="item_have[]" 
  4.   value="<?php echo $items['id']; ?>"
  5. />
  6.  
Each checkbox should use the same "name" attribute - the reason for this will be discussed later.

Note: you'll have to modify the above code to be output in your loop and also substitute the $row variable with the correct data.

Now that we have some data we can use, we can move on to processing this data.

When your form is submit, that form data is then available in either the $_GET or $_POST superglobal arrays (PHP populates them for you), depending on the the "method" attribute of your form - if you specify no method, it will default to GET. I'll assume you're using POST.

So now let's take a look at what we need to tell PHP to do.

When PHP is processing the POST data of the HTTP request, it looks to see what nodes have the same names (before I mentioned that all the checkboxes should use the same "name attribute"), and then sticks each of them into an array - a very cool thing, I think you'll agree. This leaves us with an array we can access with the $_POST variable, like so:

Expand|Select|Wrap|Line Numbers
  1. var_dump($_POST['have_item']);
  2.  
If you stick the above code in a PHP file and then tell your form to submit data to it, you'll see a human-readable representation of the array PHP created for you, hopefully populated with the items you selected!

Now, according to Code Green's post above, we need a piece of data in the format "(1,2,3,...)" to give to MySQL. We can achieve this using the implode PHP function:

Expand|Select|Wrap|Line Numbers
  1. // Items that I have, formatted appropriately
  2. $have_items_fm = implode(',', $_POST['have_item']));
  3.  
And that should be roughly all you need to know. Just stick this variable in your MySQL query string between a couple of parentheses, and you're laughing!

Let us know if you have any trouble.

11 4458
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. UPDATE get SET have = 'YES' WHERE name IN ($CheckedRows)
$CheckedRows needs to be a string of the format 'id1','id2','id3'.
So you wil need to loop through the web-form $_POST array, collecting which names were checked.
Jan 25 '11 #2
And how may that be accomplished? Sorry to say I don't really understand arrays and looping through them.
Jan 26 '11 #3
Markus
6,050 Expert 4TB
Hi, flametail.

Firstly you need to associate some value with your checkboxes so that you can read this data when your form is submit. At this point I will assume that your rows in your database have some unique ID associated with them. So your checkboxes will look something like:

Expand|Select|Wrap|Line Numbers
  1. <input 
  2.   type="checkbox" 
  3.   name="item_have[]" 
  4.   value="<?php echo $items['id']; ?>"
  5. />
  6.  
Each checkbox should use the same "name" attribute - the reason for this will be discussed later.

Note: you'll have to modify the above code to be output in your loop and also substitute the $row variable with the correct data.

Now that we have some data we can use, we can move on to processing this data.

When your form is submit, that form data is then available in either the $_GET or $_POST superglobal arrays (PHP populates them for you), depending on the the "method" attribute of your form - if you specify no method, it will default to GET. I'll assume you're using POST.

So now let's take a look at what we need to tell PHP to do.

When PHP is processing the POST data of the HTTP request, it looks to see what nodes have the same names (before I mentioned that all the checkboxes should use the same "name attribute"), and then sticks each of them into an array - a very cool thing, I think you'll agree. This leaves us with an array we can access with the $_POST variable, like so:

Expand|Select|Wrap|Line Numbers
  1. var_dump($_POST['have_item']);
  2.  
If you stick the above code in a PHP file and then tell your form to submit data to it, you'll see a human-readable representation of the array PHP created for you, hopefully populated with the items you selected!

Now, according to Code Green's post above, we need a piece of data in the format "(1,2,3,...)" to give to MySQL. We can achieve this using the implode PHP function:

Expand|Select|Wrap|Line Numbers
  1. // Items that I have, formatted appropriately
  2. $have_items_fm = implode(',', $_POST['have_item']));
  3.  
And that should be roughly all you need to know. Just stick this variable in your MySQL query string between a couple of parentheses, and you're laughing!

Let us know if you have any trouble.
Jan 26 '11 #4
I tried, and I failed. I used GET just to see what was being sent and it was index.php?have_items=1&have_items=2&submit=submit

Dunno if that's right or not, but var_dump only shows 2, or 3 if thats in the list, so always highest number? And it keeps saying implode has improper syntax.

also, can you add the stuff to my code in the first post with a few comments? I want to see if I did it right.
Jan 28 '11 #5
Markus
6,050 Expert 4TB
Woops! That's my fault. The input elements should be named the same with square brackets appended! Like this:
Expand|Select|Wrap|Line Numbers
  1. Name 1: <input type="text" name="username[]" /><br />
  2. Name 2: <input type="text" name="username[]" />
  3.  
My apologies.

Without sound rude, no, I will not write your code for you. If you'd like to show us what you've tried, then we can point out what's wrong and how you can fix it.
Jan 28 '11 #6
Ok, I have it working now :D

Now, is it possible to only show the checkbox if have=no?
Jan 28 '11 #7
Markus
6,050 Expert 4TB
Sure - just modify your SQL SELECT stuff to use a WHERE query.

Expand|Select|Wrap|Line Numbers
  1. SELECT blah,blah FROM table WHERE column = 'value'
  2.  
Jan 28 '11 #8
Here is a JavaScript solution to the problem:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body onLoad=chgtable()>
  3. <table border=1>
  4. <tr><td id="tablecell">Unchecked</td></tr>
  5. </table>
  6. <script type="text/javascript">
  7. function chgtable(){
  8.     if(document.getElementById("checkbox").checked == true){
  9.         document.getElementById("tablecell").innerHTML = "Checked"
  10.     }
  11.     else{
  12.         document.getElementById("tablecell").innerHTML = "Unchecked"
  13.     }
  14. }
  15. </script>
  16. <input type=checkbox id="checkbox" onChange=chgtable()><br />
  17. </body>
  18. </html>
Jan 28 '11 #9
Markus
6,050 Expert 4TB
Would you care to explain how that is a solution?
Jan 28 '11 #10
Neither of those is a solution. :)

I still want the row of data shown, just not the checkbox.
Jan 28 '11 #11
Markus
6,050 Expert 4TB
Sorry about that, flametail. I misread your question.

Your code would look something like (I'm making some assumptions - I haven't seen your code, so I'm not sure what you're doing):
Expand|Select|Wrap|Line Numbers
  1. if ( $item['have'] == 1 )
  2. {
  3.   echo "<input type='checkbox' name='have_item[]' value='whatever' />";
  4. }
  5.  
Jan 29 '11 #12

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

Similar topics

3
by: Michael Haas | last post by:
Hi, does anyone know of a simple function/class that will take config options: table name and the database connection stuff(or whatever) It will then print out appropriate form fields to allow...
11
by: propizzy | last post by:
Appreciate any help!!! PROBLEM: I have this form that allows the user to dynamically create additional fields (see javascript code bellow). I am trying to retrieve the values entered into these...
0
by: MattB | last post by:
Hi. I'm on my second redesign of a dynamic form I need to create and I'm looking for good examples and/or suggestions. I have a web application that is being distributed to different clients of...
3
by: Angelos | last post by:
Hello again, I have this dynamic menu and I want to change the order of the menu items... I added a column in the database wich has an integer value for ordering the menuitems. But the only way...
19
by: Alex | last post by:
Hello list This question has probably already been asked, but let me ask again I have a mysql database to which I connect with my php scripts. The database contains articles. Name, Unit_Price...
13
by: salad | last post by:
Operating in A97. I didn't receive much of a response conserning Pivot tables in Access. Pivot tables are nice, but a CrossTab will work for me too. Using a Pivot table, one is actually...
6
by: traineeirishprogrammer | last post by:
I am currently working on a project where I need to sort my MYSQL query results by different categories. How ever the code does not seem to be working properly and I spend too much time on it...
2
by: vinceboy | last post by:
Hi anybody. I am newbie here and would like to know that how can I validate both drop down menu and radio button from a dynamic display form.Something went wrong with my script.The radio button is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.