0

这是我第一次使用Mysqli,所以在数据库中添加信息时我有点迷茫。这是我的脚本。

<?php
include("config.php");

$username = $_POST['username'];
$password = $_POST['password'];
$passcheck = $_POST['password-check'];
$email = $_POST['email'];
$address = $_POST['address'];
$suite = $_POST['apt'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$passcrypt = md5($passcheck);

if($_POST['submit']){
$stmt = $db->prepare("SELECT * FROM `accounts` WHERE `username`= ?");
$stmt->bind_param('s', $username);
$stmt->execute();
if($stmt->num_rows != 0){
$final_report = "That username already exists.";
}
if($password <> $passcheck){
$final_report = "Your passwords do not match";
}
if($final_report == NULL){
$stmt = $db->prepare("INSERT INTO `accounts` (`email`, `username`, `password`, `address`, `city`, `state`, `zip`) VALUES (?, ?, ?, ?, ?, ?, ?)");
if (false === $stmt) {
    var_dump($db->error);
}
$stmt->bind_param('sssssss',$email, $username, $passcrypt, $address, $city, $state, $zip);
$stmt->execute();
}
$stmt->close();
}
?>
<form method="POST" action="">
<?php
if($final_report != NULL){
print"{$final_report}";
}
?>
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Repeat Password:</td>
<td><input type="password" name="password-check" /></td>
</tr>
<tr>
<td>E-Mail:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Apt #/Suite:</td>
<td><input type="text" name="apt" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td>State:</td>
<td><input type="text" name="state" /></td>
</tr>
<tr>
<td>Zip Code:</td>
<td><input type="text" name="zip" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>

这些是提交页面后我返回的错误。 Notice: Undefined variable: final_report in C:\xampp\htdocs\gptcode\signup.php on line 25

string(0) ""

Fatal error: Call to a member function bind_param() on a non-object in C:\xampp\htdocs\gptcode\signup.php on line 30

我显然并不担心该通知,但vardump()它什么也没显示,所以我不确定是什么导致了错误,因为bind_param()我有三次检查表和列名。如果这里的代码是正确的,那么我的问题是,如果实际表中有更多的值和列在其中列出,它会产生这样的错误,因为我没有在prepare语句中包含所有列?

4

1 回答 1

2

未定义的 var 不是 NULL 使用isset($varname)

如果您使用 PDO,则它的 bindParam 不是 bind_param

如果只使用一次,为什么要首先绑定参数,给一个数组来执行():

$stmt = $db->prepare("INSERT INTO `accounts` (`email`, `username`, `password`, `address`, `city`, `state`, `zip`) VALUES (:email, :username, :password, :address, :city, :state, :zip)");

$stmt->execute(array(
  ':email' => $mail,
  ':username' => $username,
.... and so on.
));
于 2012-11-05T02:43:10.387 回答