PHP脚本呼应部分的PHP而不是什么意图
我遇到了我创建的用于将实例插入到数据库中的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“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”; }其他{回声“帐户无法创建”; }} $ pdo = null; } catch(PDOException $ e){echo $ e-> getMessage(); }?>
这个脚本导致这个错误是什么?
你得到这个结果的唯一方法是,如果你写了:
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
如:
$pdo?>setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
因为失误。
在包含代码的第一行setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
页面开始处缺少<?php
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
上一篇: php script echoing part of the php instead of what intended
下一篇: Zend Framework: Error Reporting is shut down regardless of ini File