我正在尝试将用户添加到我的users表中,表单会询问用户名并要求用户从下拉列表中选择一个管理器。我想从managers表中的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 | manager_name | manager_dept
有人可以帮忙吗?感谢提前
答案 0 :(得分: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>
答案 1 :(得分: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;
}
}
请记住,您不希望以原始形式存储密码,因此$salt
和hashSSHA
功能。这些列在下面。
/**
* 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及其所有代码,请访问here