显示状态为0的成员列表

时间:2014-11-24 17:32:32

标签: php mysql

我想做两件事。

  1. 当用户注册时,他默认为会员,但状态为0,这表示尚未获得管理员批准。 SQL将列出状态为0的所有成员?

  2. 如果我有一个状态为零的成员列表,那么从0变为1的最佳方法是什么?我想使用下拉框或单选按钮来控制它,是要更改它的SQL ALTER

  3. SQL:

    CREATE TABLE IF NOT EXISTS `user` (
    `ID` int(11) NOT NULL AUTO_INCREMENT,
    `fname` varchar(200) DEFAULT NULL,
    `lname` varchar(200) DEFAULT NULL,
    `username` varchar(200) DEFAULT NULL,
    `password` varchar(200) DEFAULT NULL,
    `usertype` varchar(20) DEFAULT 'm',
    `status` tinyint unsigned NOT NULL DEFAULT '0',
    `email` varchar(200) NOT NULL,
    PRIMARY KEY (`ID`),
    UNIQUE KEY `email` (`email`),
    UNIQUE KEY `username` (`username`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
    

1 个答案:

答案 0 :(得分:1)

要获取状态为0的用户的所有详细信息,您需要运行此查询:

SELECT * FROM user WHERE status = 0;

要更新表,您需要使用UPDATE查询:

UPDATE user SET status = 1 WHERE username = '$username';
  //   ^table to update                       ^this needs to be set before (and shouldn't be used like this - see the script for prepared statements

一个非常基本的脚本来做你想做的事情:

<?php
 $sql = new mysqli('localhost','DB_USER','DB_USER_PASS','DB_TO_CONNECT_TO'); // you need to edit this with your real credentials / connection details

 $sql -> set_charset ( 'utf8' );
 if ($sql->connect_errno) {
    printf("Connect failed: %s\n", $sql->connect_error);
    exit();
 }

 if (isset($_POST['updateuser'])) {  // checks if you want to update someone
 $stmt = $sql->prepare("UPDATE user SET status = 1 WHERE username = ?"); //prepare a query to your DB
 $stmt->bind_param('s',$_POST['username']); //bind the value to your query
 $stmt->execute(); // run the query
 }

 $query = "SELECT username FROM user WHERE status = 0"; // show all users with status 0
 ?>
 <form name='statzerouser' method='post' action=''>
 <label for='username'>User with status 0</label>
 <select name='username' id='username'> 
 <?php
 if ($result = $sql->query($query)) {
 while ($row = $result->fetch_assoc()) {
 ?>
 <option value='<?= $row['username']; ?>'><?= $row['username']; ?></option> <!-- get all users with status 0 in a select dropdown -->
 <?php
 }
 }
 ?>
 </select>
 <input type='submit' name='updateuser' value='submit'/>
 </form>

注意:

您可能希望从一开始就学习mysqli,而不是PDO。两者都用于连接数据库。在这两种情况下,如果您找到一个教程,向您显示如下更新语句:

UPDATE user SET status = 1 WHERE username = '$username';

更改教程并找到使用预准备语句的教程!