1

我有一个来自 MySQL 的语言表,在数据库中有一行“启用”,其值为 1 或 0。1 表示启用的语言,0 表示禁用。我想用我的 html/php 表中的复选框更改这些值。

我遇到的问题是输入名称,因为它们是来自数据库的变量+选中复选框的值。

我的代码如下所示:

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

1 回答 1

1

HTML 表单中的一系列复选框都应具有相同的输入名称。服务器端接收该名称的数组。您需要首先使它们都具有相同的名称。假设您将其称为“enabledLanguages[]”。'[]' 很重要,因为这是使值作为数组发送的原因。所以你想要更像这样的东西:

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

然后将每个语言的值设为语言 ID。更改此查询:

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

所以它更像是这样的:

$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";
于 2012-10-11T17:45:38.273 回答