简体   繁体   English

将复选框保存在mysql数据库中

[英]Save checkbox in mysql database

I've updated my code. 我已经更新了我的代码。 It's working except when i select more selectboxes at a time. 除我一次选择更多选择框外,它都有效。 If i select for example 2 selectboxes at the same time and i update them, the database updates them both to 0. When i update them separately, everything goes fine. 例如,如果我同时选择2个选择框并对其进行更新,则数据库会将它们都更新为0。当我分别更新它们时,一切都会很好。

This the code right now: 现在的代码是:

<?php
include "header.php";

if($_SERVER['REQUEST_METHOD'] == 'POST')
{

    if(isset($_POST['aan']))
    {
        $doSave = implode(',',$_POST['aan']);
        mysql_query("UPDATE Modules SET 
            aan = '".mysql_real_escape_string('1')."'
            WHERE module_name IN ('".mysql_real_escape_string($doSave)."')") or die (mysql_error());

        mysql_query("UPDATE Modules SET 
            aan = '".mysql_real_escape_string('0')."'
            WHERE module_name NOT IN ('".mysql_real_escape_string($doSave)."')") or die (mysql_error());

    }
    elseif(!isset($_POST['aan']))
    {
        mysql_query("UPDATE Modules SET 
            aan = '".mysql_real_escape_string('0')."'
            WHERE module_name NOT IN ('".mysql_real_escape_string($doSave)."')") or die (mysql_error());
    }

    echo $doSave;

}

echo "<h1>Module beheer</h1>";

$getmodules = mysql_query("SELECT * FROM Modules") or die(mysql_error());

echo "<form id='module' method='post'>";
   while($modules = mysql_fetch_array($getmodules))
   {
        echo "<label>".$modules['module_name']."</label>";
        echo "<input type='hidden' name='module_name' value='".$modules['module_name']."' />";
        if($modules['aan'] == 1) 
        { 
            $set_checked = "CHECKED";
        }
        else
        {
            $set_checked = "";
        }

        echo "<input type='checkbox' name='aan[]' value=".$modules['module_name']." ".$set_checked." /><br />";
    }
   echo "<input type='submit' id='modulesubmit' name='modulesubmit' value='Opslaan'>";

echo "</form>";

include "footer.php";
?>

Please start using PDO to access a database! 请开始使用PDO访问数据库! The mysql_* functions are deprecated and not maintained anymore! mysql_ *函数已被弃用,不再维护!

status is a preserved keyword in MySQL; status是MySQL中的保留关键字; using it as column name is not very smart. 用它作为列名不是很聪明。 You can avoid it being used as keyword by using backtiks. 您可以通过使用backtiks来避免将其用作关键字。 But to avoid confusing I would take another column name. 但是为了避免混淆,我将使用另一个列名。 Let's say: aan . 假设: aan

Lets clean up your code a little bit. 让我们整理一下代码。

You've got many lines which do the same 'logic'; 您有很多行执行相同的“逻辑”。 whenever you write something multiple times in your code something is wrong. 每当您在代码中多次编写某些内容时,就会出错。 Why do you query the status seperately while the statu field itself is already in the module row? 当statu字段本身已经在模块行中时,为什么还要单独查询状态?

Then same goes for the update statement? 那么更新语句也一样吗? You just want to update the status field to 1|0. 您只想将状态字段更新为1 | 0。

if($_POST){
   if(intval($_POST['aan']) == 1 || intval($_POST['aan']) == 0){
     $aan = intval($_POST['aan']);
   }else{
     $aan = 0; //what if it's not 1|0
   }
   mysql_query("UPDATE Modules SET 
            aan = '".mysql_real_escape_string($aan)."'
            WHERE module_name = '".mysql_real_escape_string($_POST['module_name'])."'") or die (mysql_error());

)

    echo "<h1>Module beheer</h1>";

    $getmodules = mysql_query("SELECT * FROM Modules") or die(mysql_error());


    echo "<form id='module' method='post' action='*****add action parameter*****'>";
       while($m = mysql_fetch_array($getmodules)){
           {
           echo "<label name='module_name'>".$m['module_name']."</label>";
           echo "<input type='checkbox' name='aan' value='".$m['aan']."'>";
           }

       echo "<input type='submit' id='modulesubmit' name='modulesubmit' value='Opslaan'>";

    echo "</form>";

You are trying to access label value when submit your form,you cant access label value as you have define. 您试图在提交表单时访问标签值 ,但无法按定义访问标签值。 You Have to change your code as : 您必须将代码更改为:

mysql_query("UPDATE Modules SET 
            Status = '".mysql_real_escape_string('1')."'
            WHERE module_name = '".mysql_real_escape_string($_POST['uitgeschakeld'])."'") or die (mysql_error());
            echo'bhal';

and make change in following line- 并在以下行中进行更改-

**echo "<input type='checkbox' id='ingeschakeld' name='ingeschakeld' value='.$fetchmodules['module_name'].' checked></br>";**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM