如何将多个复选框值插入数据库 [英] How to Insert Multiple Checkbox Values into a Database

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

问题描述

我一直在研究这个问题,并且在网上搜索并在两天的大部分时间内都空着了.汗流,背,我可以将一个复选框值插入数据库.但是,如果我选中更多框,它仍然只会插入一个值.

I've been working on this and searching the Web and coming up empty for the better part of two days. With a lot of sweat, I can insert one checkboxed value into a DB; however, if I check more boxes, it will still only insert one value.

这是我在输入用户页面上使用的代码(不包括表单标签):

Here is my code I'm using on input user page (with form tags excluded):

  <input type="checkbox" name="skills[]" value="1"> ActionScript<br />
  <input type="checkbox" name="skills[]" value="2"> AppleScript <br />

现在,我要获取两个会话变量,以将所选技能链接到登录的用户:

Now I'm grabbing two session variables to make link the selected skill to the user who is logged on:

  $skills = $_POST['skills'];
  $id = $_SESSION['Ind_ID'];

以下是其中存在一些小错误的代码:

for ($i=0; $i<sizeof($skills);$i++) {
    // echo $skills[$i];
    $query= "INSERT INTO individual_skills(Skills_ID,Ind_ID) VALUES (" .$skills[$i]. ",
            ".$_SESSION['Ind_ID'].")";
 }

用我的回声$ skills [$ i]可以看到我正在循环播放,并且可以在屏幕上显示尽可能多的用户输入.所以,这是个好消息.

With my echo $skills[$i] I can see that I am looping and can display as many user inputs to the screen; so, that is the good news.

问题是我无法使其在数据库中插入多个值.我一次可以做一个价值;但是,不超过一个.

The issue is I can't get it to insert more than one value into a DB. I can do one value at a time; but, no more than one.

似乎我的查询非常接近.如果我选择仅选择一个复选框,它将非常完美;但是,如果我选择多个,它只会输入最后一个值(因此,如果我选择了10个值,它将把最后一个值插入数据库).

It seems my query is pretty close. It works perfect if I choose to select only one checkbox; however, if I select more than one, it only inputs the last value (so, if I have 10 values selected, it will insert the last value into the DB).

任何帮助,不胜感激.

我正在使用PHP和MySQLi

I'm using PHP and MySQLi

基于对循环的反馈进行了更新 这是更新的循环.我在循环外有查询,现在尝试追加到我的初始查询中.错误表示查询语法有问题.

UPDATED BASED ON FEEDBACK FOR LOOP Here is the updated loop. I have the query outside the loop, and now trying to APPEND to my initial query. Error says something is wrong with the syntax of the query.

$query= "INSERT INTO individual_skills(Skills_ID,Ind_ID) VALUES (" .$skills[$i]. ",
                                                        ".$_SESSION['Ind_ID'].")";

  for ($i=0; $i<sizeof($skills);$i++) {
  //echo $skills[$i];
  $query.= " VALUES (" .$skills[$i]. ",".$_SESSION['Ind_ID'].")";
  }

推荐答案

考虑一下您的代码在做什么.在每个循环中,您都在重新定义$query变量,以便结束时,$query的最终值将是您最后一次检查的想法.

Think about what your code is doing. Each loop, you're redefining the $query variable, so that when you get to the end, the final value of $query will be the last think you checked.

如果您仍想使用与编写的循环逻辑相同的方法(这可能不是最有效的方法),则每次在 循环中都必须执行查询,因此您编写的每个查询都将被执行.

If you still want to use the same loop logic that you have written, which is probably not the most efficient way, then you'd have to execute the query each time within the loop, so that each query you write will be executed.

更确切地说,您似乎还没有发现分配变量值和将变量传递给函数执行之间的区别,这是造成混淆的原因.

More precisely, the confusion seems to be stemming from the fact you haven't quite discovered the difference between assigning a value to a variable and passing a variable to a function for execution.

这篇关于如何将多个复选框值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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