Da9L Posted March 18, 2013 Share Posted March 18, 2013 (edited) Hello .. I need some help with this .. I have a user database where an admin can add/delete users and search for them on a website.. What i want is a possibility to update user details.. Its a little hard to explain but heres a screenshot As you can see i have some fields that is editable .. Those fields are generated based on what you search for on the page before.. On the top row you can see two buttons: "Slet"(delete) and "Gem" (save) .. On the first collum all to the left you can see a checkbox.. As it is now, a user can mark this checkbox and click delete, which deletes the marked users from the database .. What i want also is if a user has edited some information in the fields AND marked the checkbox to the left of the edited fields AND clicked "Gem" (save) the rows with the marked users should be updated with the new information in the database.. I just have no idea how to do this.. Heres the code for the delete button: if($_POST['delete']){ $idList = join (',', array_map('intval', $_POST['checkbox'])); $sql = "DELETE FROM `users` WHERE `userid` IN ($idList)"; mysql_query($sql); echo "* - De markerede brugere blev slettet!"; include "includes/redirection_admin.php"; } At first i thought it would be as simple as to just change the sql query to UPDATE instead but that doesnt work .. This is what i have made: $idList = join (',', array_map('intval', $_POST['checkbox'])); $sql = "UPDATE `users` SET `fornavn`='$userid' IN '$fornavn', `efternavn` IN '$efternavn', `cprnr` IN '$cprnr', `adresse` IN '$adresse', `postnr` IN '$postnr', `by` IN '$by', `tlfnr` IN '$tlfnr', `email` IN '$email', `userid` IN '$userid' WHERE `userid` IN ($idList)"; mysql_query($sql); echo "* - De markerede brugere fik opdateret deres oplysninger!"; But that doesnt work .. The fields doesnt change Can anyone assist ? Edited March 18, 2013 by Da9L Quote Link to comment https://forums.phpfreaks.com/topic/275805-update-multiple-rows-with-multiple-fields-from-a-form-into-mysql-based-on-checkboxes/ Share on other sites More sharing options...
Barand Posted March 18, 2013 Share Posted March 18, 2013 WHERE id IN ($idlist) is OK for an an update query only if you want to give all those records the same values, which I don't believe is the case here. You really need to check the mysql manual for the correct syntax for using SET in an update. Quote Link to comment https://forums.phpfreaks.com/topic/275805-update-multiple-rows-with-multiple-fields-from-a-form-into-mysql-based-on-checkboxes/#findComment-1419342 Share on other sites More sharing options...
Da9L Posted March 19, 2013 Author Share Posted March 19, 2013 WHERE id IN ($idlist) is OK for an an update query only if you want to give all those records the same values, which I don't believe is the case here. You really need to check the mysql manual for the correct syntax for using SET in an update. Sorry about the mysql query it was just a quick out the head placeholder.. I've decided to not use checkboxes as i simply cant figure out how to . So now im going to try this instead.. When each line has been generated from the search results a form with two buttons will also be generated.. Quote Link to comment https://forums.phpfreaks.com/topic/275805-update-multiple-rows-with-multiple-fields-from-a-form-into-mysql-based-on-checkboxes/#findComment-1419483 Share on other sites More sharing options...
Zane Posted March 19, 2013 Share Posted March 19, 2013 (edited) The key to accomplishing something like this is to populate every row with every vital piece of info needed to find it in the database. Additionally, the use of jQuery's (Javascript) AJAX functions can make this tremendously easier if you aren't doing that already. Anyway, When you are populating this table that you showed in your screenshot, you would insert the vital info in the source code of that table. <tr class="user" data-id="1234"> <td><input type='text' name='username' value='zane'/></td> <td><input type='text' name='email' value='zane@domain.com'/></td> </tr> Given the above format, we can use jQuery to extract certain info and send that data to an external PHP script upon changing the text box value.... or however you want to trigger the change. $("tr.user input").change(function() { var $userid = $(this).parent("tr.user").data('id'); $field = $(this).attr('name'); $newVal = $(this).val(); var update = $.ajax({ url: "script.php", type: "POST", data: {table : "users", id : userid, field : field, newVal : newVal} }); }); Then, on your script.php page you can find this information in POST and do with it what you will.None of this code is tested... it is simply an off the top of my head suggestion. Edited March 19, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/275805-update-multiple-rows-with-multiple-fields-from-a-form-into-mysql-based-on-checkboxes/#findComment-1419486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.