adduser_process.php没有向MySQL表添加信息

时间:2012-10-09 01:29:48

标签: php mysql forms

我的网站上有一个用户创建表单,无法将任何新用户数据添加到相关的MySQL表中。我真的不太清楚它为什么不添加数据,而且它没有抛出任何会告诉我出错的错误。

我目前有以下用户创建表单:

<form name="adduser_form" action="process/adduser_process.php" method="post">
    Username:<input id="username" type="test" name="username" />
    Email:<input id="email" type="text" name="email" />
    Password:<input id="password" type="password" name="password" />
    <input id="add_usr" type="button" value="Add User" onclick="formhash(this.form, this.form.password);" />
</form>

点击&#34;添加用户&#34;按钮,formhash JS创建一个哈希密码,然后将表单提交给adduser_process.php:

function formhash(form, password) {
    var p = document.createElement("input");
    form.appendChild(p);
    p.name = "p";
    p.type = "hidden";
    p.value = hex_sha512(password.value);
    password.value = "";
    form.submit();
}

adduser_process.php的工作原理如下(故意遗漏标签):

include '../includes/lgn_connect.php';
include '../includes/functions.php';
sec_session_start(); 

$password = $_POST['p']; 

$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));

$password = hash('sha512', $password.$random_salt);

if($insert_stmt = $mysqli->prepare("INSERT INTO users (username, email, password, salt) VALUES (?, ?, ?, ?)")) {
$insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt); 

$insert_stmt->execute();
}

1 个答案:

答案 0 :(得分:2)

第一个错误是您已将类型设置为test而不是text用户名

<input id="username" type="test" name="username" />     //should be
<input id="username" type="text" name="username" />

第二个错误是if条件,你没有在条件

中添加额外的()
if($insert_stmt = $mysqli->prepare("INSERT INTO users (username, email, password, salt) VALUES (?, ?, ?, ?)"))   //should be

if(($insert_stmt = $mysqli->prepare("INSERT INTO users (username, email, password, salt) VALUES (?, ?, ?, ?)")))

第三个错误是sec_session_start();不是启动会话的功能,它应该是

session_start();