php script echoing part of the php instead of what intended

I'm having trouble with php script that I've created to insert instances into a database, however I'm getting a trivial output and i dont know how to fix it. the code is:

<?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();
    }


?>

i would expect the output to be something like " Account created ". Instead the output I'm getting this error:

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(); } ?>

whats going wrong with this script to cause this?


The only way you'd get that output is if you had written:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

as:

$pdo?>setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

by mistake.


在包含代码的第一行setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);页面开始处缺少<?php setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

链接地址: http://www.djcxy.com/p/58910.html

上一篇: html 5输入类型的电子邮件$

下一篇: PHP脚本呼应部分的PHP而不是什么意图