填充复选框,然后将选择更新到 mysql [英] populate checkboxes and then update selection to mysql

查看:46
本文介绍了填充复选框,然后将选择更新到 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个表单,其中的复选框是从 MySQL 数据库中填充的:

I currently have a form where checkboxes are populated from a MySQL database using:

//possible array values in row(value)
$values = array('F', 'R', 'C', 'T', 'S');
// $names =  array('football', 'rugby', 'cricket', 'tennis', 'swimming')
$sql = "SELECT value, description
    FROM table_1
    WHERE ID = '$userid'";
$result = mysqli_query($conn, $sql);
while ($row= mysqli_fetch_array($result)) {
$db_values[] = $row['0'];

然后使用以下方法预先选择复选框:

Then the checkboxes are pre-selected using:

<?php
foreach ($values as $value) {
$selected = in_array($value, $db_values) ? ' checked="checked"' : null;
echo "<input type='checkbox' name='sports[]' id ='sport' value=" . $value . $selected . "/>" . $value . "<br />";
} ?>

这部分工作正常,输出:

This part is working okay and gives an output of:

<input type='checkbox' name='sports[]' id ='sport' value='F' checked='checked'/>F

第 1 部分:我试图进一步编辑它,以便在复选框旁边显示名称而不是像这样的值:

Part 1: I am trying to edit it further though in order to display the name beside the checkbox instead of the value like such:

<input type='checkbox' name='sports[]' id ='sport' value='F' checked='checked'/>football

有人对此有任何想法吗?

Anyone any ideas on this?

第 2 部分:我现在尝试更新选中/取消选中的任何复选框,并将值更新到同一个数据库表中.我试过了:

Part 2: I am now trying to update whatever checkboxes are checked/de-checked and update the values into the same database table. I have tried:

$sports = $_POST["sports"];
for ($i=0; $i < sizeof($sports); $i++) {
    $sql = "UPDATE table_1
            SET value='$sports[$i], ID='$userid'
            WHERE ID='$userid'";
    $result = mysqli_query($conn, $sql);

但我可能完全以错误的方式处理这部分,但我不知道如何让它工作.

But I am probably going about this part completely the wrong way but I cant figure out how to get it to work.

我不断收到错误:

Duplicate entry 'ID-value' for key 'PRIMARY'

对于我提交的每一个选择

For every choice I submit

提前感谢您的帮助.

编辑......................

EDIT..............................

Table_1 像这样

Table_1 like this

ID   |  Sport
John |  F
John |  C
John |  R
Paul |  R

如果我勾选一个复选框,然后单击更新,就会出现问题.如果用户没有条目并且我选中了它似乎可以正常更新的框ID-Sport的组合是主键

The problem arises if I leave a checkbox ticked and then click update. If the user has no entries and I check the box it seems to update fine A combination of ID-Sport is the Primary key

推荐答案

你不应该在一个页面中多次使用相同的 id.

You should not same id more that once in a page.

并用 '

<?php
   foreach ($values as $value) {
   $selected = in_array($value, $db_values) ? ' checked="checked"' : null;
   echo "<input type='checkbox' name='sports[]' id ='sport' value='" . $value ."' " . $selected . "/>" . $value . "<br />";
} ?>

像这样创建数组

$arr['F'] = "Football";
$arr['R'] = "Rugby";
$arr['C'] = "Cricket";

$arr[$value] instead of $value

编辑

<?php
    $arr['F'] = "Football";
    $arr['R'] = "Rugby";
    $arr['C'] = "Cricket";
    foreach ($values as $value) {
        $selected = in_array($value, $db_values) ? ' checked="checked"' : null;
        echo "<input type='checkbox' name='sports[]' id ='sport' value=" . $value . $selected . "/>" . $arr[$value] . "<br />";
    }
?>

这篇关于填充复选框,然后将选择更新到 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆