Jump to content

Update multiple rows with multiple fields from a form into MySQL based on checkboxes


Recommended Posts

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

 

 

Unavngivet12.jpg

 

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 by Da9L

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.

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

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 by Zane
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.