简体   繁体   English

如何将复选框值保存到数据库中

[英]how to save checkbox value into database

hi i have a checkbox and i want to save its value into DataBase if it checked and save different text if its not checked. 嗨,我有一个复选框,我想将其值保存到数据库(如果已选中),并保存其他文本(如果未选中)。

i tried to this code , it works but if i have 2 checkboxe it doesn't work any more. 我尝试使用此代码,它可以工作,但是如果我有2个复选框,它将不再工作。

tried code in view.php : 在view.php中尝试代码:

$ch = SELECT check FROM users;
if($ch=="checked"){
    echo "checked";
}else{
    echo "not checked";
}

html : html:

<form action="action.php" method="post" id="form">
<p>checkbox: <input type="checkbox" name="check" value="checked" /></p>
<input type="submit" value="submit" />
</form>

action.php : action.php:

<?php

define('DB_NAME', 'users');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect : ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can not use '.DB_NAME . ' :  ' . mysql_error());
}

$check =$_POST['check'];

$sql = "INSERT INTO users (check) VALUES ('$check')";

echo "your informations were sent to our database click to come back";

mysql_query($sql);
mysql_close();
?>

view.php : view.php:

<?php
$dbname = "users";
$username = "root";
$password = "";
$servername = "localhost";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

     while($row = $result->fetch_assoc()) {
         echo "checkbox : ". $row["check"];
     }
} else {
echo "<a class='btsc'>no one registrated if you want register complete the form</a>";
}

$conn->close();
?> 

db : D b :

http://i.stack.imgur.com/xmoVc.jpg http://i.stack.imgur.com/xmoVc.jpg

As has been said, checkbox values are not set when they are not checked. 如前所述,如果未选中复选框值,则不会设置它们。

Here is a little work around I have used: 这是我使用过的一些解决方法:

 <input type="hidden" name="check" value="false" />
 <input type="checkbox" name="check" id="check" value="true" />

Now if the checkbox is checked then the hidden element with the same name is overwritten. 现在,如果选中此复选框,则具有相同名称的隐藏元素将被覆盖。

page1.html page1.html

HTML 的HTML

<form action="page2.php" method="post" id="form">
<p>checkbox: <input type="checkbox" name="check" id="check" value="checked" /></p>
<input type="submit" value="submit" />
</form>

page2.php page2.php

PHP 的PHP

<?php
// Take the value of the checkbox
$varcheck = $_POST['check'];
// Check if the value of $_POST is correct
echo "The value is: $varcheck";
?>

Connect now to your database and run an INSERT INTO QUERY, like this: 现在连接到数据库并运行INSERT INTO QUERY,如下所示:

INSERT INTO users (check) VALUES ('$varcheck');

$_POST['check'] will be undefined if the user submitted the form with the checkbox unchecked. 如果用户提交了未选中复选框的表单,则$_POST['check']是不确定的。

So, if you want to store different values depending if it's checked, use something like isset($_POST['check']) . 因此,如果要根据是否检查而存储不同的值,请使用isset($_POST['check'])

if ( isset($_POST['check']) ) {
    $check = "some value";
} else { 
    $check = "another value";
}

$sql = "INSERT INTO users (check) VALUES ('$check')";

I also strongly urge you to use PDO or mysqli to perform all MySQL database operations - noticed you used mysql_query() which has been deprecated for major security reasons!! 我也强烈建议您使用PDOmysqli来执行所有MySQL数据库操作-注意您使用的mysql_query()出于主要的安全原因而已弃用!! (see the big red box at http://php.net/manual/en/function.mysql-query.php ) (请参见http://php.net/manual/en/function.mysql-query.php上的红色大框)

    // this, is the html index.php file
        <form action="test.php" method="post" id="form">
            <p><input type="checkbox" name="myCheck" data-toggle="toggle" data- 
            on="absent" data-off="présent" data-onstyle="danger" data-offstyle="secondary"></p>
            <input type="submit" value="submit" />
        </form>

// Now, we are redirecting... test.php file
        <?php
        // Take the value of the checkbox
           $var = $_POST['myCheck'];
           if ($var == 'on') {
              $var = 1;
              echo "The value is: $var</br>";
           }else {$var = 0;
               echo "The value is: $var</br>";
            }

            $sql = "INSERT INTO users (check) VALUES ('$var')";
            //..
            //...
            // value will be 1 or 0;
        ?>

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

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