【问题标题】:Submitting Multiple Checkboxes Values to Database向数据库提交多个复选框值
【发布时间】:2012-10-29 21:16:21
【问题描述】:

我对 PHP 很陌生,我正在寻找一种将多个复选框的值提交到数据库中的方法!现在我已经设法让复选框显示为选中,如果数据库中的值 = 1,否则它将被取消选中。

下面是我目前的代码。

<?php
    $query = "SELECT * FROM users_achievements_xbox WHERE user_id = '$userID' && game_id = '$gameID' ORDER BY achievement_id ASC";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)) {   // Start looping table row
    $userID = $row['user_id'];
    $gameID = $row['game_id'];
    $achievementID = $row['achievement_id'];
    $achievementName = $row['achievement_name'];
    $achievementDescription = $row['achievement_description'];
    $achievementValue = $row['achievement_value'];

    $check = mysql_query("SELECT * FROM users_achievements_xbox WHERE user_id = '$userID' && game_id = '$gameID' && achievement_id = '$achievementID'");
    $row2 = mysql_fetch_assoc($check);
    $achievement_locked = $row2['achievement_locked'];

    if($achievement_locked == "0")
    {
        $checked = 'checked="checked"';
    }
    else
    {
        $checked = "";
    }
?>
    <tr align="center" bgcolor="#eeeeee">
        <td> <img src="images/achievements/<?php echo $gameID; ?>/<?php echo $achievementID; ?>.jpg" /> </td>
        <td> <?php echo $achievementName; ?> </td>
        <td> <?php echo $achievementDescription; ?> </td>
        <td> <?php echo $achievementValue; ?> </td>
        <td> <input type="checkbox" name="checkbox" value="0" <?php echo $checked ?> /> </td>
    </tr>

<?php
} // end of while loop
?>

我正在考虑尝试通过 isset 执行此操作,但我不想要一个循环的提交按钮在我的数据库中的“achievement_locked”中,当我只希望选中的复选框在选中时更改为 0,如果不选中则更改为 1!

感谢您的帮助!

【问题讨论】:

标签: php mysql database checkbox


【解决方案1】:

这是一个例子。得到理解。我没有逃脱 POST 变量。确保你这样做。

HTML 文件:

<form name="form1" method="post" action="test.php">
  <p>
    <input name="colors[]" type="checkbox" id="colors[]" value="0">
    Red 
    <input name="colors[]" type="checkbox" id="colors[]" value="0">
    Blue 
    <input name="colors[]" type="checkbox" id="colors[]" value="0">
    Green</p>
  <p> 
    <input name="colors[]" type="checkbox" id="colors[]" value="0">
    Yellow 
    <input name="colors[]" type="checkbox" id="colors[]" value="0">
    Brown </p>
    <input type="submit">
</form>

这是你的 php 文件应该有的。

<?php

$colors = $_POST['colors'];

$red = isset($colors[0]) ? 1 : 0;
$blue = isset($colors[1]) ? 1 : 0;
$green = isset($colors[2]) ? 1 : 0;
$yellow = isset($colors[3]) ? 1 : 0;
$brown =isset($colors[4]) ? 1 : 0;



// query
$sql = "INSERT INTO table (red,blue,green,yellow,brown) VALUES (:red,:blue,:green,:yellow,:brown)";
$q = $conn->prepare($sql);
$q->execute(array(':red'=>$red,
                    ':blue'=>$blue,
                    ':green'=>$green,
                    ':yellow'=>$yellow,
                  ':brown'=>$brown));
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 2013-06-06
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2013-06-07
    • 1970-01-01
    相关资源
    最近更新 更多