我在创建用于将实例插入数据库的 php 脚本时遇到问题,但是我得到了一个微不足道的输出,我不知道如何修复它。代码是:
<?php
try{
$user = 'root';
$pass = null;
$pdo = new PDO('mysql:host=localhost; dbname=divebay', $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$username = $_POST['username'];
$password = sha1($_POST['password']);
$location = %_POST['location'];
$email = $_POST['email'];
$name = $_POST['fname'] . " " . $_POST['surname'];
$check = $pdo->prepare('SELECT * FROM user WHERE username=?');
$check->bindValue(1, $username);
$check->execute();
if($check->fetch(PDO::FETCH_OBJ)){
echo "Account name already exists";
}
else{
$stmt = $pdo->prepare('INSERT INTO user(username, password, location, email, name)
VALUES(:username, :password, :location, :email, :name)');
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->bindParam(':location', $location, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
if($stmt->execute()){
echo "Account created";
}
else{
echo "Account could not be created";
}
}
$pdo = null;
}catch(PDOException $e){
echo $e->getMessage();
}
?>
我希望输出类似于“创建帐户”。相反,我收到此错误的输出:
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $username = $_POST['username']; $password = sha1($_POST['password']);
$location = %_POST['location']; $email = $_POST['email']; $name = $_POST['fname'] 。“”。$_POST['姓氏']; $check = $pdo->prepare('SELECT * FROM user WHERE username=?');
$check->bindValue(1, $username); $检查->执行();
if($check->fetch(PDO::FETCH_OBJ)){ echo "账户名已经存在"; } else{ $stmt = $pdo->prepare('INSERT INTO user(username, password, location, email, name) VALUES(:username, :password, :location, :email, :name)'); $stmt->bindParam(':username', $username, PDO::PARAM_STR); $stmt->bindParam(':password', $密码,PDO::PARAM_STR);
$stmt->bindParam(':location', $location, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
if($stmt->execute()){ echo "账户创建"; } else{ echo "无法创建账户"; } } $pdo = null; }catch(PDOException $e){ echo $e->getMessage(); } ?>
这个脚本出了什么问题导致这个?