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

时间:2012-07-02 05:20:16

标签: php mysql

我的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>

和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 />';
}
?>

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

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

1 个答案:

答案 0 :(得分:3)

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