Javascript PHP:将多个复选框值插入到一个 MySQL 列中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27578858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:21:02  来源:igfitidea点击:

PHP : insert multiple check boxes values into one MySQL column

javascriptphpjquerymysqlcheckbox

提问by Sohail Sardar

I want to write a simple PHP function to insert values of 10 and 20 check boxes. Now, the issue is: should I insert all values in a single column of MySQL table or should I go with the separate table?

我想编写一个简单的 PHP 函数来插入 10 和 20 复选框的值。现在,问题是:我应该在 MySQL 表的单个列中插入所有值还是应该使用单独的表?

My main goal is to insert the values of multiple checkboxes into MySQL and then update them. If I checked 7 checkboxes, and after some time I want to update from 7 to 5, how it will remove values from table column?

我的主要目标是将多个复选框的值插入 MySQL,然后更新它们。如果我选中了 7 个复选框,一段时间后我想从 7 更新到 5,它将如何从表列中删除值?

Please help me with some kind simple PHP example and what type of MySQL fields should I add, becuase I want to insert checkbox value which should in digital and the label of check boxes in other field.

请帮助我提供一些简单的 PHP 示例以及我应该添加什么类型的 MySQL 字段,因为我想插入数字的复选框值和其他字段中的复选框标签。

Here is the HTML I have

这是我拥有的 HTML

<form method="post" action="">
   Games You Like: <br/>
   <input type="checkbox" name="games[]" value="1"><label>Football</label><br>
   <input type="checkbox" name="games[]" value="2"><label>Basket Ball</label><br>
   <input type="checkbox" name="games[]" value="3"><label>Pool</label><br>
   <input type="checkbox" name="games[]" value="4"><label>Rugby</label><br>
   <input type="checkbox" name="games[]" value="5"><label>Tennis</label><br>
   <input type="checkbox" name="games[]" value="6"><label>Cricket</label><br>
   <input type="checkbox" name="games[]" value="7"><label>Table Tennis</label><br>
   <input type="checkbox" name="games[]" value="8"><label>Hockey</label><br>
   <input type="submit" name="submit" value="submit">
</form>

回答by Mahendra Pumbhadiya

Try this whole example,

试试这整个例子,

Table Structure

表结构

CREATE TABLE IF NOT EXISTS `games` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `game_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
<?php
include_once("yourconfig.php"); //include your db config file
extract($_POST);
$check_exist_qry="select * from games";
$run_qry=mysql_query($check_exist_qry);
$total_found=mysql_num_rows($run_qry);
if($total_found >0)
{
    $my_value=mysql_fetch_assoc($run_qry);
    $my_stored_game=explode(',',$my_value['game_name']);
}

if(isset($submit))
{
    $all_game_value = implode(",",$_POST['games']);
    if($total_found >0)
    {
        //update
        $upd_qry="UPDATE games SET game_name='".$all_game_value."'";
        mysql_query($upd_qry);

    }
    else
    {
        //insert
        $ins_qry="INSERT INTO games(game_name) VALUES('".$all_game_value."')";
        mysql_query($ins_qry);
    }
}

?>
<form method="post" action="">
Games You Like: <br/>
    <input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
    <input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
    <input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>
    <input type="checkbox" name="games[]" value="4" <?php if(in_array(4,$my_stored_game)){echo "checked";}?>><label>Rugby</label><br>
    <input type="checkbox" name="games[]" value="5" <?php if(in_array(5,$my_stored_game)){echo "checked";}?>><label>Tennis</label><br>
    <input type="checkbox" name="games[]" value="6" <?php if(in_array(6,$my_stored_game)){echo "checked";}?>><label>Cricket</label><br>
    <input type="checkbox" name="games[]" value="7" <?php if(in_array(7,$my_stored_game)){echo "checked";}?>><label>Table Tennis</label><br>
    <input type="checkbox" name="games[]" value="8" <?php if(in_array(8,$my_stored_game)){echo "checked";}?>><label>Hockey</label><br>
    <input type="submit" name="submit" value="submit">
</form>

this is just basic example and query i have added in this example, you can learn from this basic example and i think this is very useful for you... if useful than give correct answer for this solution

这只是我在此示例中添加的基本示例和查询,您可以从这个基本示例中学习,我认为这对您非常有用...

Db Table Stored Output

数据库表存储输出

Form Output

表格输出

回答by imran qasim

use implode function to convert returned array into a string an then insert it into database as normal

使用内爆函数将返回的数组转换为字符串,然后正常插入到数据库中

if(isset($_POST['submit'])){
   $result = implode(",",$_POST['games']);

}

}

hopefully this will help you. Regards Imran Qasim

希望这会帮助你。问候伊姆兰·卡西姆

回答by chrki

I put the game name into the brackets and changed their values to true:

我将游戏名称放入括号中并将它们的值更改为 true:

<form method="post" action="">
Games You Like: <br/>
<input type="checkbox" name="games[Football]" value="true"><label>Football</label><br>
<input type="checkbox" name="games[Basket]" value="true"><label>Basket Ball</label><br>
<input type="checkbox" name="games[Pool]" value="true"><label>Pool</label><br>
<input type="checkbox" name="games[Rugby]" value="true"><label>Rugby</label><br>
<input type="checkbox" name="games[Tennis]" value="true"><label>Tennis</label><br>
<input type="checkbox" name="games[Cricket]" value="true"><label>Cricket</label><br>
<input type="checkbox" name="games[Table]" value="true"><label>Table Tennis</label><br>
<input type="checkbox" name="games[Hockey]" value="true"><label>Hockey</label><br>
<input type="submit" name="submit" value="submit">
</form>

After submission this will result in an array like this:

提交后,这将导致这样的数组:

$_POST["games"] = Array ( [Tennis] => true [Cricket] => true [Table] => true ) 
// all other values false

You can easily store and later manipulate this array if you store it as an JSON string in your database. For that you need a single varchar or text column.

如果将它作为 JSON 字符串存储在数据库中,则可以轻松存储和稍后操作此数组。为此,您需要一个 varchar 或 text 列。

json_encode($_POST["games"])
// {"Tennis":"true","Cricket":"true","Table":"true"}

回答by sujeet gupta

Please you try this

请你试试这个

<?php
if(isset($_POST['submit']))
{
//in here you get games array
   $mygames = $_POST['games'];  
}

?>


<form method="post" action="">
Games You Like: <br/>
<input type="checkbox" name="games[]" value="1"><label>Football</label><br>
<input type="checkbox" name="games[]" value="2"><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3"><label>Pool</label><br>
<input type="checkbox" name="games[]" value="4"><label>Rugby</label><br>
<input type="checkbox" name="games[]" value="5"><label>Tennis</label><br>
<input type="checkbox" name="games[]" value="6"><label>Cricket</label><br>
<input type="checkbox" name="games[]" value="7"><label>Table Tennis</label><br>
<input type="checkbox" name="games[]" value="8"><label>Hockey</label><br>
<input type="submit" name="submit" value="submit">
</form>

回答by Gavin

Firstly very straight to point, I prefer doing names like this from different checkboxes

首先非常直截了当,我更喜欢从不同的复选框中做这样的名字

BasketBall;Cricket;Tennis

You can do that by

你可以这样做

implode(";",$array);

and then write that in mySQL table...

然后将其写入 mySQL 表中...

To get those values Just use

要获得这些值只需使用

$array = explode(";",$urVariable);

For different values use

对于不同的值使用

numberOne = $array[1]; // returns Cricket

to change any value use Just get the values and rewrite in ";" form..

更改任何值使用只需获取值并在“;”中重写 形式..

I like this as it is simple to implement and do job perfectly!

我喜欢这个,因为它易于实施并且完美地完成工作!