0

我正在尝试将用户添加到我的用户表中,表单要求输入用户名并要求用户从下拉列表中选择经理。我想从管理器表中的 manager_id 列填充现有管理器。但是我不知道该怎么做。到目前为止,这是我的代码:

<?php

if (isset($_POST['add'])) {
require_once 'login.php'; 

  $OK = false;

  $conn = new mysqli ($host, $user, $password, $database) or die("Connection Failed");

  $stmt = $conn->stmt_init();

  $sql = 'INSERT INTO users (user_name, user_manager, user_creation_date)
          VALUES(?, ?, NOW())';
  if ($stmt->prepare($sql)) {

    $stmt->bind_param('ss', $_POST['user_name'], $_POST['user_manager']);

    $stmt->execute();
    if ($stmt->affected_rows > 0) {
      $OK = true;
    }
  }

  if ($OK) {
    header('Location: add_user_confirm.php');
    exit;
  } else {
    $error = $stmt->error;
  }
}
?>
<!DOCTYPE HTML>
<html>
<head>

<title>Add new user</title>
</head>

<body>
<h1>Add new user</h1>
<?php if (isset($error)) {
  echo "<p>Error: $error</p>";
} ?>
<form id="form1" method="post" action="">
  <p>
    <label for="user_name">user name:</label>
    <input name="user_name" type="text" class="widebox" id="user_name">
  </p>
  <p>
    User manager: <select name="user_manager" size "1">

  </p>
  <p>
    <input type="submit" name="add" value="add_user" id="add">
  </p>
</form>
</body>
</html>

我的“经理”表如下所示: manager_id | 经理姓名 | 经理部门

有人可以帮忙吗?提前致谢

4

2 回答 2

2

像这样的东西应该工作

<p>
    User manager: <select name="user_manager" size "1">
    <?php $stmt->close();
    $stmt2 = $conn->stmt_init();
    $selectQ = 'select * from managers';
    $stmt2->prepare($selectQ);
    $stmt2->execute();
    $result = $stmt2->get_result();
    while($resultRow = $result->fetch_array(MYSQLI_ASSOC))
        echo '<option value="$resultRow[manager_id]">$resultRow[manager_name]</option>';
    $result->close();
    $stmt2->close();
    ?>
</p>
于 2013-02-10T21:52:02.783 回答
0

下面是添加用户的示例。

   /**
 * Storing new user
 * returns user details
 */
public function storeUser($name, $email, $password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
    // check for successful store
    if ($result) {
        // get user details
        $uid = mysql_insert_id(); // last inserted id
        $result = mysql_query("SELECT * FROM users WHERE uid = $uid");
        // return user details
        return mysql_fetch_array($result);
    } else {
        return false;
    }
}

请记住,您不想以原始形式存储密码,因此使用$saltandhashSSHA功能。下面列出了这些。

    /**
 * Encrypting password
 * @param password
 * returns salt and encrypted password
 */
public function hashSSHA($password) {

    $salt = sha1(rand());
    $salt = substr($salt, 0, 10);
    $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
    $hash = array("salt" => $salt, "encrypted" => $encrypted);
    return $hash;
}

/**
 * Decrypting password
 * @param salt, password
 * returns hash string
 */
public function checkhashSSHA($salt, $password) {

    $hash = base64_encode(sha1($password . $salt, true) . $salt);

    return $hash;
}

这些功能将有助于确保安全。此外,如果您想了解更多相关信息或查看整个 API 及其所有代码,请访问此处

于 2013-02-10T22:17:07.537 回答