PHP-General - Update mysql with multiple drop down and checkbox value
Discuss Update mysql with multiple drop down and checkbox value in the PHP Development forum on Dev Shed. Update mysql with multiple drop down and checkbox value PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 7
Time spent in forums: 2 h 12 m 32 sec
Reputation Power: 0
PHP-General - Update mysql with multiple drop down and checkbox value
Hi all
I'm new to the forum and php,
I'm trying to update a mysql TABLE (A) with data from the output of another table TABLE (B). I'm using a checkbox on each row and a select option (ID) generated from a column from table TABLE (A). when the user selects a row (using the checkbox) or multiple rows and hits the update button, I'd like to have the drop down option (ID) they choose to update the appropriate row in TABLE(A).
I think I've solved most of it but the problem I have is that the select option drop down is on each row and so has the same name, so just sends an array which is fine but I need the individual value for each row associated with each checkbox ID.
Heres my code if you have any ideas. I'd like to apologise if it's a bit rough around the edges.
At the moment I'm not UPDATING the data I'm just trying to display the data to see how it's working.
Heres the code for processing the form:
PHP Code:
<?php
if ((isset($_POST["MM_update4"])) && ($_POST["MM_update4"] == "updatefactorycheckbox")) {
if(isset($_GET['orderby'])){
$orderby = $_GET['orderby'];
$query_Recordset1 = "SELECT * FROM sales__factory_quotes WHERE engineer_name='".$_SESSION['MM_Username']."' ORDER BY ".mysql_real_escape_string($orderby)." ASC";
}
//default query
else{
$query_Recordset1 = "SELECT * FROM sales__factory_quotes WHERE engineer_name='".$_SESSION['MM_Username']."' ORDER BY primary_ID ASC";
}
$query = "SELECT project_ID FROM sales__projects WHERE engineer_name='".$_SESSION['MM_Username']."' ORDER BY project_ID ASC";
$result = mysql_query($query) or die("Error in in query. ".mysql_error());
?>
<!--OPTION DROP DOWN TO SELECT ID TO UPDATE-->
<select name = 'project_id[]'><option value = '<?=$option?>'>Select ID/New Project</option>
Posts: 760
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
Quote:
Originally Posted by pcraig23
I think I've solved most of it but the problem I have is that the select option drop down is on each row and so has the same name, so just sends an array which is fine but I need the individual value for each row associated with each checkbox ID.
Hi there
welcome to the forums; please forgive me as I'm not sure I understand the problem 100% ... have you tried using the checkbox ID as the select name?
For example in the loop that creates the SELECT / table ...
(the actual value would be determined by which OPTION the user chose before they submitted the form)
You can use a foreach / for loop to grab the actual values from the $_POST['sel'] array.
Anyway, I might be waaaaaaay off the mark so apologies if I am!
EDIT > OK so I entered the code below into my webserver:-
__________________
The number for UK Emergencies is changing, the new number is 0118 999 881 999 119 7253
"For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
- George Orwell, 1984
Last edited by badger_fruit : October 22nd, 2012 at 09:52 AM.
Reason: Corrected my poor HTML skillz and wrapped code in PHP tags, oops!
Posts: 7
Time spent in forums: 2 h 12 m 32 sec
Reputation Power: 0
Hi bager_fruit
sorry for the delay
Thanks for the reply I think I understand what you did but I may have it completely wrong, what your code seems to do is to create each row with multiple select options according to how many rows there are in the first place. I tried it without the 'for' code but it still returns the last value. What I want to be able to do is select a value from the option drop down on the left then choose the row with the checkbox and insert all the data from that individual row into a new database table, because all the rows are generated automatically the select option on the left only returns the last value.
I'd like to include a screenshot but I can't include a url as a new user
Posts: 760
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
I don't get it, where is the SELECT box of which you speak? I see two TEXT boxes, one which reads "Select ID/Project" and the other which reads "10036".
Am I correct in thinking that you want a list of all projects to be shown to the user and, as in your screenshot, for each project the user has a pencil icon which, when clicked, will display the (currently hidden) text input boxes. When the user has made changes, they click in the "update" checkbox to indicate that 'I wish to save my changes to this record'.
Finally, the user has two additional options not shown on your screenshot:-
1. Add new project
2. Submit changes
When the user clicks on Save changes, you want to determine which projects have their "update" checkbox selected and only update those?
Posts: 7
Time spent in forums: 2 h 12 m 32 sec
Reputation Power: 0
Sorry the text boxes are drop down those are just two different values I selected.
The 2 rows you see on the screenshot are from table A (user specific) but the drop down on the left is generated from table B (main database).
When the user checks one or more checkboxes and hits update (which u cant see on the screenshot) it updates table B with the content using the ID (value) of the drop down shown.
the pencil was just an edit function for the user to update the row before submit. the code isn't included it works fine.
Posts: 7
Time spent in forums: 2 h 12 m 32 sec
Reputation Power: 0
Quote:
Originally Posted by badger_fruit
OK so what is in the $_GET or $_POST (depending on your form's METHOD)?
Add the line
print_r($_GET);
or
print_r($_POST);
into your processing page and then copy here the output,
ta
ok here's the output when I select both rows I added 10030 as the first option and 10031 as the second, you can see it only picks up the second number, the checkbox returns the id fine.
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
?>
So once you have your arrays (e.g. "Project ID", "Customer", Project Name", "Notes") you can grab the values from them with a foreach loop, something like this ...
PHP Code:
foreach ($_POST['project_id'] as $key => $value) {
...
}
Posts: 7
Time spent in forums: 2 h 12 m 32 sec
Reputation Power: 0
Hi badger_fruit.
Yeah I get that part about the [] I'm using that to get the id from the checkbox, what I cant seem to do is isolate each value of the drop down per row when the user updates more than one checkbox there's obviously an array but I want to select one option per checkbox ID not all which is what I currently get
PHP Code:
if ((isset($_POST["MM_update4"])) && ($_POST["MM_update4"] == "updatefactorycheckbox")) {
if (isset($_POST['submit'])) {
echo "<pre>";
print_r($_POST);
echo "</pre>";
}
?>
OK, you can see this demonstrated here --> http://badgerfruit.co.uk/examples/multi_form.php
In an example submission, this was the resulting $_POST ...
This is of course, a problem! We can't say for sure which checkboxes were selected (we can only see that three were). So what's the solution? Well, as I see it you need to give the checkbox fixed names ... we can just change one line ...
EDIT : We need to give it a unique name, not fixed - sorry. We can derive this unique name by simply adding the number of the current loop iteration into it (in this case, $i) like so ...
Again, the result can be seen from the multi_form2.php linked to above but in summary, we can now find what checkboxes were set and what the appropriate select was set to!
Quote:
1 ~ 1
1
3 ~ 1
3
4 ~ 1
0
Ta dar
Last edited by badger_fruit : November 5th, 2012 at 02:01 PM.
Reason: Clarification
Posts: 7
Time spent in forums: 2 h 12 m 32 sec
Reputation Power: 0
ah ok I get it. Thank you badger_fruit for your help!
I re-wrote my code and it works, I used the table row values as the array values for the checkbox, the foreach was the key, simple when it clicked in my head. Here's my code for future ref, sorry it's a bit raw.
PHP Code:
<?php
if ((isset($_POST["MM_update4"])) && ($_POST["MM_update4"] == "updatefactorycheckbox")) {
Posts: 760
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
Hi there, really pleased you have it working and if you're the only one who works on the code then who really cares if it's messy It may be wise though to at least TRY to keep it neat and make sure to put in lots of comments (so when your boss says to you after the Christmas do "can you change XY and Z please").
Now, the next thing you need to get around to is using PDO instead of the old "mysql" libraries. There's a bunch of tutorials out there and of course, this forum for when you get stuck (notice I said "when" and not "if" hehe).
Posts: 7
Time spent in forums: 2 h 12 m 32 sec
Reputation Power: 0
Quote:
Originally Posted by badger_fruit
Now, the next thing you need to get around to is using PDO instead of the old "mysql" libraries. There's a bunch of tutorials out there and of course, this forum for when you get stuck (notice I said "when" and not "if" hehe).
Anyway, you're welcome regarding the help!
Yep probably get a book on it...I prefer books, I can make lots of sub-notes etc...by the way I tried to up your reputation, I presume I cant do that as it wouldn't let me