maikelsabido
maikelsabido

Reputation: 1327

How to update mysql database fields in groups (using GROUP BY)

I have a table named youi in my database. All fields in the table already contain values except for the aff and desc fields. See image below. Youi table

Now, I have a form in my HTML page (See image below) where I want to update the desc and aff fields according to their camp. I have not yet dealt with this kind of setup before. I've been thinking how to do this for almost a day now but still can't seem to find a perfect solution. Could you give me ideas or solutions on how to achieve this? I'm using PHP and mySQL.

Thanks in advance!

enter image description here

Upvotes: 1

Views: 614

Answers (3)

maikelsabido
maikelsabido

Reputation: 1327

I finally found a solution to this problem. I used combination of jQuery, AJAX, PHP and mySQL for it to work.

All <select> have the same id. The same for <input> and <label>. Here's a sample of my HTML code:

<select id="youiaff">
    <option>1001</option>
    <option>1007</option>
    <option>1009</option>
    <option>1013</option>
    <option>1017</option>
    <option>1018</option>
    <option>1022</option>
</select>
<input id="youidesc" type="text" />
<label id="youicamp"></label>
<button type='button' class='btn btn-success saveyouiid'>Save</button>

What I did next was to create a jQuery code that will get all the values of <select>, <input> & <label> and put each of them in an array. I used their ids as identifiers. Here's the code:

var desc = $("input[id='youidesc']")
          .map(function(){return $(this).val();}).get();
var aff = $("select[id='youiaff']")
          .map(function(){return $(this).val();}).get();
var camp = $("label[id='youicamp']")
          .map(function(){return $(this).text();}).get();

Then, I passed the variables to the PHP script using AJAX:

$.ajax({
    type: 'post',
    url: 'saveyouiid.php',
    data: {
        desc:desc,
        aff:aff,
        camp:camp,
    },
    success:function(data){

    }
});

This codes will be executed upon clicking the save button. So the full jQuery/AJAX for this would be:

$('.saveyouiid').click(function(){
    var desc = $("input[id='youidesc']")
          .map(function(){return $(this).val();}).get();
    var aff = $("select[id='youiaff']")
          .map(function(){return $(this).val();}).get();
    var camp = $("label[id='youicamp']")
          .map(function(){return $(this).text();}).get();

    $.ajax({
        type: 'post',
        url: 'saveyouiid.php',
        data: {
            desc:desc,
            aff:aff,
            camp:camp,
        },
        success:function(data){

        }
    });
});

The PHP script (saveyouiid.php) will then accept the values sent via AJAX. These values are arrays. What I did next was I combined the arrays to form a multidimensional array. Then, I get the individual values and perform the mySQL query. Here's what the script looks like:

<?php
$con = mysqli_connect("localhost","imu_fryu","frankyouiIMU2013","imu_frankyoui");
if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$aff = $_POST['aff'];
$desc = $_POST['desc'];
$camp = $_POST['camp'];

$arr = array_map(null, $aff, $desc, $camp);

foreach($arr as $array)
{
    $aff = $array[0];
    if ($aff == ">> Select Affiliate ID <<"){
        $affID = "0";
    }else{
        $affID = $aff;
    }
    $desc = $array[1];
    $camp = $array[2];

    $sql1 = "UPDATE youi SET aff = '$affID', descr = '$desc' WHERE camp = '$camp'";

    if (!mysqli_query($con,$sql1)) {
        die('Error: ' . mysqli_error($con));
    }

}

mysqli_close($con);
?>

I hope this could help someone in the future. :)

Upvotes: 0

danny2327
danny2327

Reputation: 97

You can use CASE, WHEN and THEN in a loop to make the one query. This is a statement I created using a simple for loop to update a bunch of captions on a group of photos.

UPDATE Images SET caption = CASE imgID WHEN 389 THEN 'a' WHEN 390 THEN 'sdf' WHEN 391 THEN 'safasasadf' WHEN 392 THEN 'fs' WHEN 393 THEN 'dfdsf' WHEN 394 THEN 'sfdf' END WHERE imgID IN (389,390,391,392,393,394); 

Hope that helps

aff = (case when somefield='slkd' then yyy end), desc = (case when somefield='slkdfdsd' then xxx end)

Upvotes: 1

Filipe Silva
Filipe Silva

Reputation: 21657

The easiest way i see is using a different UPDATE for each of those lines. You could do that in a loop in php where you construct your update with the values of aff, desc and campaign for each line.

The sql would be:

UPDATE tableName
SET aff  = varAffiliate,
    desc = varDescription
WHERE campaign = varCampaign;

The php part i'm not of much help, sorry.

Upvotes: 1

Related Questions