我有一个与'process_register.php'对话以注册用户的表单。该文件与名为“user”的类进行对话。
<form method="post" action="process_register.php">
User Name: <input type="text" name="userName" maxlength="32"><br>
Password: <input type="password" name="userPassword" maxlength="32"><br>
<input type="submit">
</form>
process_register.php
<?php
// process_register.php
include('userclass.php');
$newUser = new User;
// Call the registerUser() method, passing in the required variables
$newUser->registerUser($userName, $userPassword);
// If it was an error, the class will kill the script, if not, it will reach this point
$newUser->displayUserInfo();
?>
userclass.php
<?
// process_register.php
class User {
var $userID,
$userName,
$userPassword,
$dbHost,
$dbUser,
$dbName,
$dbPass,
$dbUserTable;
function registerUser($userName, $userPassword) {
// Connect to database
$dbLink = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
if(!$dbLink) die("Could not connect to database. " . mysql_error());
// Select database
mysql_select_db($this->dbName);
$query = "insert into $this->dbUserTable values (NULL, \"$userName\", \"$userPassword\")";
$result = mysql_query($query);
// Test to make sure query worked
if(!$result) die("Query didn't work. " . mysql_error());
// Get the user ID
$this->userID = mysql_insert_id();
// Close database connection
mysql_close($dbLink);
// Assign the values to the data members
$this->userName = $userName;
$this->userPassword = $userPassword;
} // End registerUser()
function displayUserInfo() {
echo '<b>User ID: </b>' . $this->userID . '<br>';
echo '<b>User Name: </b>' . $this->userName . '<br>';
echo '<b>User Password: </b>' . $this->userPassword . '<br>';
} // End displayUserInfo()
问题是当process_register.php调用'displayUserInfo'时,它在DB中为新用户创建了一个插槽,但它是空白的。
非常感谢任何帮助
答案 0 :(得分:0)
您需要在行“
”之前的“process_register.php”文件中为数组$ _POST中的变量赋值$newUser->registerUser($userName, $userPassword);
。例如,
$userName = $_POST['userName'];
$userPassword = $_POST['userPassword'];
但不要忘记过滤用户输入数据。
答案 1 :(得分:0)
您是否关注旧教程?
在线,$ newUser-&gt; registerUser($ userName,$ userPassword); 它假设$ userName,$ userPassword是全局变量。旧版本的PHP默认情况下已注册了全局变量。在当前版本的PHP中,您必须这样做:
$ newUser-&gt; registerUser($ _ REQUEST ['userName'],$ _REQUEST ['userPassword']);
另外,请确保对添加到查询中的所有数据使用mysql_real_escape_string() - 如果不这样做,则可以使用SQL注入。
答案 2 :(得分:0)
您可能还想更改insert语句以放入MD5密码而不是明文。
$userPassword = md5($userPassword); before the insert
不是“必要的”,但是一个很好的安全措施,供将来参考。