如何将复选框中的值插入到 php 中的 MySQL 中? [英] How do I insert the value from a checkbox into MySQL in php?

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

问题描述

我正在关注我的 sql 字段

i am having following my sql field

表格

`cbox1` tinyint(1) NOT NULL default '1',
`cbox2` tinyint(1) NOT NULL default '1',
`cbox3` tinyint(1) NOT NULL default '1',
`cbox3` tinyint(1) NOT NULL default '1',

index.html

<form method="post">
<input type="hidden" name="blah" value="blah">

<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">
<input type="checkbox" name="cbox[]" value="1">

<button type="submit">Submit</button>
</form>

demo.php

$sql="INSERT INTO freetrail (cbox1, cbox2, cbox3,cbox4)VALUES ('$cbox1', '$cbox2', '$cbox3','$cbox4')";

我想做的是:

a) 如果复选框被选中,我想用 1 更新相应的字段

a) If the checkbox is checked, I want to update the appropriate field with 1

b) 如果未选中复选框,我想插入带有 0 的字段

b) If the checkbox is unchecked, I want to insert the field with 0

我怎样才能实现我的目标

How can i achieve my goal

提前致谢

推荐答案

首先我会改变这些

    <form method="post">
            <input type="hidden" name="blah" value="blah">

            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">
            <input type="checkbox" name="cbox[]" value="1">

            <button type="submit">Submit</button>
    </form>

由于您无法告诉服务器当前检查了哪一个,如果仅检查其中一个,您将在 $_POST 中得到类似的信息

As you wont be able to tell severside which one is checked currently you'll get something like this if only one of them is checked, in the $_POST

  array(
       'blah' => 'blah', //your hidden field
       'cbox' => array(
            0 => 1
        )
  );

如果 2 被选中

   array(
       'blah' => 'blah', //your hidden field
       'cbox' => array(
            0 => 1
            1 => 1
        )
  );

但问题是哪两个?因此,您需要更改这些名称或值

But the question is which 2? So you will either need to change the names of these or the values

        <input type="checkbox" name="cbox[]" value="1">
        <input type="checkbox" name="cbox[]" value="2">
        <input type="checkbox" name="cbox[]" value="3">
        <input type="checkbox" name="cbox[]" value="4">
        <input type="checkbox" name="cbox[]" value="5">

        <input type="checkbox" name="cbox1" value="1">
        <input type="checkbox" name="cbox2" value="1">
        <input type="checkbox" name="cbox3" value="1">
        <input type="checkbox" name="cbox4" value="1">
        <input type="checkbox" name="cbox5" value="1">

或者如果你想让事情更复杂

OR if you want to further complicate things

        <input type="checkbox" name="cbox[cbox1]" value="1">
        <input type="checkbox" name="cbox[cbox2]" value="1">
        <input type="checkbox" name="cbox[cbox3]" value="1">
        <input type="checkbox" name="cbox[cbox4]" value="1">
        <input type="checkbox" name="cbox[cbox5]" value="1">

根据您选择的内容,您需要检查它是否已设置,如果未选中该复选框,则不会将其发送到服务器.有几种方法可以解决这个问题,具体取决于您如何命名或执行这些值.最好的方法是简单地使用

Depending on what one you pick, you will need to then check if it is set, if the checkbox is not checked it wont be sent to the sever. There are a few ways around this depending how you name or do the values. The best approach is simply using

   $cbox1 = isset( $_POST['cbox1'] ) ? 1 : 0; //if you rename them
   ///or -- ( or something similar for each if you change the value )
   if( in_array( 1, $_POST['cbox'] )){
       $cbox1 = 1
   }else{
       $cbox1 = 0;
   }

现在您选择做什么应该取决于复选框的数量是已知的、固定的数量还是动态的.如果它是一个固定数字,我会更改名称(第二个选项),因为它更干净,并且更容易处理服务器端.

Now which one you pick to do should depend on if the number of checkboxes is known, a fixed number, or dynamic. If it's a fixed number I would change the name ( the 2nd option ) as it is cleaner and will be much easier to deal with server side.

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

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