PHP插入数据与一次多个ID数据库? [英] php insert data into database with multiple ids at once?

查看:147
本文介绍了PHP插入数据与一次多个ID数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格。内部,一些复选框present的。所以,当有人选择一些复选框(可以说,我们选择了两个复选框磁盘空间,处理器)上保存和点击。那么它应该保存在这样的数据库

I have a form. Inside that a number of checkbox present. So when someone selects some checkboxes(lets say we have selected two checkboxes Disk Space, Processor) and clicks on save. Then it should save in the database like this

id  attribute_name
1    Disk space
2    Processor


<form id="formID" method="post" action="" enctype="multipart/form-data">
  <input type="checkbox" name=""  value="Disk space"/>Disk space<br />
  <input type="checkbox" name=""  value="Color"/>Color<br />
  <input type="checkbox" name=""  value="Processor"/>Processor<br />
  <input type="submit" name="save" id="save" value="save"/>
</form>

所以,有人可以告诉我如何做到这一点?
我做了我的插入查询像这样

So can someone tell me how to do this? I had made my insert query like this

INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$attribute_values."')";

但在一列这个输入所有选定的属性。

but this one entered all the selected attributes in a single column.

推荐答案

这个怎么样也会做任何插入查询之前验证数据。

How about this it will also validate the data before doing any insert query.

<form id="formID" method="post" action="" enctype="multipart/form-data">
  <input type="checkbox" name="type[]"  value="Disk space"/>Disk space<br />
  <input type="checkbox" name="type[]"  value="Color"/>Color<br />
  <input type="checkbox" name="type[]"  value="Processor"/>Processor<br />
  <input type="submit" name="save" id="save" value="save"/>
</form>

然后在PHP中,code将确保,如果有人只是不提交任何检查它不会尝试执行查询。

Then in PHP, the code will make sure if someone just submit without checking anything it will not try to execute the query.

if(isset($_POST["save"]) && isset($_POST["type"])){
        $types = $_POST["type"];
        if(sizeof($types) > 0 ){
            foreach($types as $type){
                    $qry= '';
                    $qry = "INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$type."')";
                    // execute the above query

            }
        }
    }   

这篇关于PHP插入数据与一次多个ID数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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