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

时间:2015-06-01 15:41:21

标签: php mysql

嗨,我有一个复选框,我想将其值保存到DataBase中,如果它已选中并保存不同的文本(如果未选中)。

我尝试了这段代码,但是如果我有2个复选框,它就不再起作用了。

在view.php中尝试了代码:

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

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:

<?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:

<?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:

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

4 个答案:

答案 0 :(得分:4)

如前所述,未选中复选框值时未设置复选框值。

这是我用过的一些小工作:

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

现在,如果选中该复选框,则会覆盖具有相同名称的隐藏元素。

答案 1 :(得分:1)

如果用户提交了未选中复选框的表单,则

$_POST['check']将被取消定义。

因此,如果您想根据选中的内容存储不同的值,请使用isset($_POST['check'])之类的内容。

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

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

强烈强烈建议您使用PDOmysqli来执行所有MySQL数据库操作 - 注意到您使用mysql_query()已被弃用以获得主要安全性原因! (参见http://php.net/manual/en/function.mysql-query.php上的大红框)

答案 2 :(得分:0)

page1.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

<强> PHP

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

立即连接到您的数据库并运行INSERT INTO QUERY,如下所示:

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

答案 3 :(得分:-1)

    // 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;
        ?>