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

时间:2023-04-21
本文介绍了PHP:将多个复选框值插入到一个 MySQL 列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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

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

这是我的 HTML

你喜欢的游戏:<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>网球</label><br><input type="checkbox" name="games[]" value="6"><label>Cricket</label><br><input type="checkbox" name="games[]" value="7"><label>乒乓球</label><br><input type="checkbox" name="games[]" value="8"><label>Hockey</label><br><input type="submit" name="submit" value="submit"></表单>

解决方案

尝试整个示例,

表结构

如果不存在`games`,则创建表(`id` int(12) NOT NULL AUTO_INCREMENT,`game_name` varchar(255) 非空,主键(`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

<?phpinclude_once("yourconfig.php");//包括你的数据库配置文件提取($_POST);$check_exist_qry="从游戏中选择 *";$run_qry=mysql_query($check_exist_qry);$total_found=mysql_num_rows($run_qry);如果($total_found > 0){$my_value=mysql_fetch_assoc($run_qry);$my_stored_game=explode(',',$my_value['game_name']);}如果(isset($提交)){$all_game_value = implode(",",$_POST['games']);如果($total_found > 0){//更新$upd_qry="更新游戏设置游戏名称='".$all_game_value."'";mysql_query($upd_qry);}别的{//插入$ins_qry="INSERT INTO games(game_name) VALUES('".$all_game_value."')";mysql_query($ins_qry);}}?><form method="post" action="">你喜欢的游戏:<br/><input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>足球</label><br><input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>篮球</标签><br><input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>池</label><br><input type="checkbox" name="games[]" value="4" <?php if(in_array(4,$my_stored_game)){echo "checked";}?>><label>橄榄球</label><br><input type="checkbox" name="games[]" value="5" <?php if(in_array(5,$my_stored_game)){echo "checked";}?>><label>网球</label><br><input type="checkbox" name="games[]" value="6" <?php if(in_array(6,$my_stored_game)){echo "checked";}?>><label>板球</label><br><input type="checkbox" name="games[]" value="7" <?php if(in_array(7,$my_stored_game)){echo "checked";}?>><label>乒乓球</label><br><input type="checkbox" name="games[]" value="8" <?php if(in_array(8,$my_stored_game)){echo "checked";}?>><label>曲棍球</label><br><input type="submit" name="submit" value="submit"></表单>

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

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?

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?

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.

Here is the HTML I have

<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>

解决方案

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

这篇关于PHP:将多个复选框值插入到一个 MySQL 列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Laravel 刀片复选框 下一篇:如何使用jQuery获取所有选中复选框的值

相关文章

最新文章