将枚举类型变量存储到数据库中

时间:2021-10-29 23:25:23

I have my registerUsers.html as below

我有我的registerUsers.html如下

<html>
<head>
<title>userlogin</title>
</head>
<body>
<h1><center>User login</center></h1>
<p>
<strong><font color= "green"> REGISTER HERE </font></strong>
<br>
<form method="post" action="registerUsers.php">
<p><strong>ID:</strong><br/>
<input type="text" name="id"/></p>
<p><strong>FirstName:</strong><br/>
<input type="text" name="firstname"/></p>
<p><strong>Lastname:</strong><br/>
<input type="text" name="lastname"/></p>
<p><strong>Username:</strong><br/>
<input type="text" name="username"/></p>
<p><strong>Password:</strong><br/>
<input type="password" name="password"/></p>
Admin <input type="radio" name="type" value="admin" /><br />
Author <input type="radio" name="type" value="author" checked="checked"/>
<p><input type="submit" name="submit" value="Register"/></p>
</form>
</p>

</body>
</html>

and registerUsers.php as below

和registerUsers.php如下

<?php
include("dbconnect.php");
$con=new dbconnect();
$con->connect();
if(isset($_POST['submit'])) {
       $type=0;

        if($_POST['type'] =="admin")
        $type = 1;
        if($_POST['type'] =="author")
        $type = 0;

    $sSql = "INSERT INTO users 
         ( id, first_name, last_name,username, password, type)
         VALUES ('$_POST[id]', '$_POST[firstname]', '$_POST[lastname]', '$_POST[username]', PASSWORD(\"$_POST[password]\"), $type)";

    mysql_query($sSql);

    echo '<h2>USER REGISTERED</h2><br />';
}
?>

When I am selecting 'author' radio button nothing is getting stored in the 'type' variable of the database. If I select admin, 'admin' is stored in type variable. How can I fix this so that 'author' can be stored in the 'users' table of the database.

当我选择'author'单选按钮时,没有任何内容存储在数据库的'type'变量中。如果我选择admin,'admin'存储在type变量中。我该如何解决这个问题,以便“作者”可以存储在数据库的“用户”表中。

Any thoughts or help would be appreciated. Thanks!

任何想法或帮助将不胜感激。谢谢!

1 个解决方案

#1


3  

If you made a type Enum in the database with the accepted values 'Author' and 'Admin', you shouldn't pass 0 and 1 to it. You pass either 'Admin' or 'Author', and the database will treat it like 0 and 1, but to you it says 'admin' and 'author'.

如果您在数据库中使用可接受的值“作者”和“管理员”创建了类型枚举,则不应将0和1传递给它。您传递“管理员”或“作者”,数据库会将其视为0和1,但对您说“管理员”和“作者”。

#1


3  

If you made a type Enum in the database with the accepted values 'Author' and 'Admin', you shouldn't pass 0 and 1 to it. You pass either 'Admin' or 'Author', and the database will treat it like 0 and 1, but to you it says 'admin' and 'author'.

如果您在数据库中使用可接受的值“作者”和“管理员”创建了类型枚举,则不应将0和1传递给它。您传递“管理员”或“作者”,数据库会将其视为0和1,但对您说“管理员”和“作者”。