【问题标题】:How do I insert the value from a checkbox into MySQL in php? [duplicate]如何在 php 中将复选框中的值插入 MySQL? [复制]
【发布时间】:2014-10-15 09:10:11
【问题描述】:

我正在关注我的 sql 字段

表格

`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 更新相应的字段

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

我怎样才能实现我的目标

提前致谢

【问题讨论】:

  • 未选中的复选框在发布表单时不会发送任何内容。您必须检查哪些具有价值,然后将其余的设置为 0。
  • 我怎样才能做到这一点,亲爱的
  • 你可以给所有的cbox设置一个默认值0
  • 你能分享一些你试过的代码吗?
  • 我所做的只是谷歌你的问题标题,我遇到了多种方法来做到这一点。你有没有做过任何研究?

标签: javascript php html mysql


【解决方案1】:

首先我会改变这些

    <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 中得到类似的结果

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

如果有 2 个被选中

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

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

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

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

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

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

   $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;
   }

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

【讨论】:

  • 这个问题是重复的。不要发布答案。有多个帖子具有这些完全相同的答案。请尽量保持社区清洁
【解决方案2】:

如果 HTML 组件在这些排列中

<input type="checkbox" name="chkBx1" value="1">
<input type="checkbox" name="chkBx2" value="2">

在 demo.php 中

function checkStatus($elementID) {
    if (array_key_exists($elementID, $_POST)) {
        $status = 1;
    } else {
        $status = 0;
    }
    return $status;
 }


 sql="INSERT INTO freetrail (cbox1, cbox2, cbox3,cbox4)VALUES ($this->chkEditStatus('chkBx1'),($this->chkEditStatus('chkBx1'), ($this->chkEditStatus('chkBx2'), ($this->chkEditStatus('chkBx3'),($this->chkEditStatus('chkBx4'))";

【讨论】:

    猜你喜欢
    • 2011-09-08
    • 2016-01-27
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 2015-07-07
    • 2011-09-06
    相关资源
    最近更新 更多