[英]Submitting Multiple Checkboxes Values to Database
I'm quite new to PHP and I'm looking for a way to submit a Value of Multiple Checkboxes into a Database! 我对PHP很陌生,正在寻找一种将多个复选框的值提交到数据库中的方法! Right now I have managed to get the Checkboxes to display as Checked if the the Value in the Database = 1 otherwise it will be unchecked.
现在,如果数据库中的值= 1,我已经设法使复选框显示为“已检查”,否则将取消选中该复选框。
Below is the current code that I have. 下面是我当前的代码。
<?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
?>
I was thinking about trying to do it via isset but I don't want a looped Submit button I can't seem to find how to wrap the Checkbox in a form for that but when I tested it quickly that way every value in "achievement_locked" in my Database became 0 when I only want the selected Checkboxes to change to 0 if checked or 1 if not! 我当时正在考虑通过isset尝试执行此操作,但我不希望使用环状的Submit按钮,我似乎无法找到如何将Checkbox包裹在表单中的方法,但是当我以这种方式快速对其进行测试时,“ achievement_locked”中的每个值如果我只希望将选中的复选框更改为0(如果未选中)则为1,则数据库中的“”变为0!
Thanks for any help! 谢谢你的帮助!
This is one example. 这是一个例子。 GEt the understanding.
获得理解。 I have not escaped the POST variables.
我没有转义POST变量。 Make sure you do.
确保你这样做。
HTML File: 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>
This is what your php file should have. 这是您的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));
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.