0

MYSQL表结构

Name    Type            Collation         Attributes    Null    Default
code    varchar(2)      utf8_unicode_ci                  No     
name    varchar(255)    utf8_unicode_ci                  No     
checkbox  tinyint(1)                                     Yes       NULL

PHP

$countryiso = mysql_query("SELECT distinct name as name FROM location_country where code NOT IN('A1','A2','AP','EU') order by name");
echo '<table>';
echo '<th>Country</th><th> Add/Remove </th>';
while ($row = mysql_fetch_assoc($countryiso)) {
 echo '<tr>';
 echo '<td>'. $row['name'] . '</td>';
 echo '<td><input type="checkbox"></td>';
 echo '</tr>';
}
 echo '</table>';

当我检查 php 后端中的复选框时,我需要将列复选框中的值从 0 更新为 1。我如何仅使用 php 和 mysql 执行此操作?请不要Jquery。

4

1 回答 1

2

如果您不想使用 JavaScript,则需要在表单末尾使用 SUBMIT 按钮:

$countryiso = mysql_query("SELECT distinct name as name FROM location_country where code NOT IN('A1','A2','AP','EU') order by name");
echo '<form action="" method="post">';
echo '<table>';
echo '<th>Country</th><th> Add/Remove </th>';
while ($row = mysql_fetch_assoc($countryiso)) {
 echo '<tr>';
 echo '<td>'. $row['name'] . '</td>';
 echo '<td><input type="checkbox" name="check[]" value="' . $row['code'] . '"></td>';
 echo '</tr>';
}
 echo '<tr><td>&nbsp;</td><td><input type="submit"></td></tr>';
 echo '</table>';
 echo '</form>';

然后:

if(isSet($_POST['check']) && $_POST['check'])
{
    foreach($_POST['check'] as $check)
    {
        mysql_query("UPDATE yourtable SET checkbox = 1 WHERE code = '" . mysql_real_escape_string($check) . "'");
        // any message of success
    }
}

不要忘记mysql_将来会被弃用和删除。为此使用PDOmysqli_

于 2013-09-22T11:20:19.237 回答