1

I have a table of languages from MySQL and in database there is a row "enabled" that has values 1 or 0. 1 for enabled language, 0 for disabled. I would like to change those values with checkboxes in my html/php table.

Problems I encounter are with input names since they are variable from database + values of checked checkboxes.

My code looks like this:

require_once ('../mysqli_connect.php'); // Connect to the db.

if(isset($_POST['submit']))
{
$lang_q="SELECT * FROM language";
$lang_r = @mysqli_query ($dbc, $lang_q); // Run the query.
while($lang_row = mysqli_fetch_array($lang_r, MYSQLI_ASSOC)){
    $lang_code2[]=$lang_row['lang_code'];
    $language[]=$lang_row['language'];
    $langid2[]=$lang_row['lang_id'];
    $lang_active2[]=$lang_row['enabled'];
}
$language2=$_POST[$language];
$lang_active=$_POST[$lang_active2];
$langid=$_POST[$langid2];
$m="test";
if(isset($language2)){
    foreach ($lang_active as $key => $value) {
        $value="1";
    }
    //$lang_active='1';
    $m.="-uspjeh";
}

$q="UPDATE language SET enabled='$lang_active' where lang_id='$langid' ";
$r = @mysqli_query($dbc, $q); // Run the query.
if($r){
    $success=true;
    require_once ('includes/login_functions.inc.php');
    $url = absolute_url();
    $url = absolute_url("language-manager.php?success=update");
    header("Location: $url");
    $m.="-da";
}   
else{
    $s ="<br />GREŠKA UPDATE";
    echo mysqli_error($r);
    $m.="-ne";
}
}
$page_title = PAGE_TITLE84;
include_once('includes/header.php');?>
<?php

$lang_q="SELECT * FROM language";
$lang_r = @mysqli_query ($dbc, $lang_q); // Run the query.
echo $m.'-dadda';
echo '
<form action="language-manager.php" method="POST" name="language-manager">
    <table class="languages">
    <tr>
        <td>'.LANGUAGE_R.'</td>
        <td>'.ACTIVE_R.'</td>
    </tr>
    ';
    while($lang_row = mysqli_fetch_array($lang_r, MYSQLI_ASSOC)){
        $lang_code2=$lang_row['lang_code'];
        $language=$lang_row['language'];
        $lang_active=$lang_row['enabled'];
        $langid=$lang_row['lang_id'];
        echo '<tr>
            <td class="lang"><img src="../flags/'.$lang_code2.'.png" />'.$language.'</td>
            <td>
                <input type="checkbox" name="'.$language.'" id="'.$langid.'" value="'.$lang_active.'"';
                    if($lang_active=='1'){
                        echo ' checked="checked"';
                    }
                echo '>
            </td>
        </tr>';
    }
    echo '</table>
    <input type="submit" name="submit" value="Update" >
</form>
</div>';

mysqli_close($dbc);
4
  • exactly how would $language2=$_POST[$language]; work? $language is an ARRAY at that point and cannot be used an array key. ditto for $lang_active2, ditto for $langid in your UPDATE query. you're updating using the text Array`, not an id, blah blah balh. Your code is fundamentally broken. Commented Oct 11, 2012 at 17:39
  • You could also try a php/ajax design. Might simplify things a tad: w3schools.com/php/php_ajax_database.asp Commented Oct 11, 2012 at 17:55
  • to Marc B - I have used "arrays" as last resort to test my IF - only when arrays are used I get $m messages printed. However, without arrays there are no $m messages but query goes through (I believe) since after "Submit" url changes to "...php?success=update". Commented Oct 11, 2012 at 22:06
  • To Joshua Kaiser - thank you, I will do that. Commented Oct 11, 2012 at 22:06

1 Answer 1

1

A series of checkboxes in an HTML form all should have the same input name. The server side receives an array by that name. You need to start by making them all the same name. Let's say you call it 'enabledLanguages[]'. The '[]' is important, as this is what makes the value send as an array. So you want something more like this:

<input type="checkbox" name="enabledLanguages[]" id="'.$langid.'" value="'.$lang_active.'"';
                    if($lang_active=='1'){
                        echo ' checked="checked"';
                    }
                echo '>

Then make the values of each the language id. Change this query:

$q="UPDATE language SET enabled='$lang_active' where lang_id='$langid' ";

so it's more like this:

$langids = implode(',', $_POST['enabledLanguages']);
// sets each row in this table to 0 (false) if the lang_id isn't IN the $langids list
// and sets to 1 (true) if it is IN the $langids list
$q="UPDATE language SET enabled= lang_id IN($langids) > 0";
Sign up to request clarification or add additional context in comments.

12 Comments

Thank you Joshua, I am just confused with new query. To me it doesn't seem complete. $q="UPDATE language SET enabled= lang_id IN($langids) > 0"; you say enabled= and after that should go $lang_active, correct? and shouldn't there be where?
Getting somewhere. I have renamed checkboxes to have equal names like you suggested. Also I have used this for query: $langid2[]=$_POST['enabledLanguages']; $langids = implode(',', $langid2); $q="UPDATE language SET enabled= lang_id IN($langids) > 0"; The result is that it remembers checked box but deletes others. For instance, I have languages English, German, Croatian, French. If I check English and update all is ok. So on the page load it loads English as checked. Second update for German checked updates German language but clears English (or any other). How to keep those checked values?
Is both English and German checked in your form when you submit the second time? They'll need to be. If they are, they should both show up in your enabledLanguages and be included in the imploded $langids strings.
My bad. Change the name of the field to enabledLanguages[]
PS, the "Warning Cannot modify header information" is probably showing up because you are using some kind of output buffering. It's just a consequence of dumping data earlier than your framework expects.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.