gpt4 book ai didi

php - 向数据库提交多个复选框值

转载 作者:行者123 更新时间:2023-11-30 23:26:20 41 4
gpt4 key购买 nike

我是 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 来完成它,但我不想要一个循环的提交按钮我似乎无法找到如何将 Checkbox 包装在一个表单中但是当我以这种方式快速测试每个值时当我只希望选中的复选框更改为 0(如果选中)或 1(如果未选中)时,我数据库中的“achievement_locked”变为 0!

感谢您的帮助!

最佳答案

这是一个例子。得到理解。我没有转义 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));
?>

关于php - 向数据库提交多个复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13129815/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com