2

我在创建用于将实例插入数据库的 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(); } ?>

这个脚本出了什么问题导致这个?

4

3 回答 3

2

获得该输出的唯一方法是编写:

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

作为:

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

因为失误。

于 2012-07-30T04:24:24.640 回答
2

你有一个而%不是$on%_POST['location']

建议: 我也强烈建议将 PDO 函数包装到一个类中。以下是我个人在每个项目中使用的内容:

将此保存到它自己的文件中(例如:sql.class.php)

<?php 

class SqlIt{
    public $Sql;
    public $Response;
    private $Host;
    private $DBname;
    private $User;
    private $Pass;
    public $NumResults;

    public function __construct($Sql, $type, $vars){
        if($vars == ""){
            $vars = array();
            }
        try{
        $DB = $this->db_connect();
        $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $STH = $DB->prepare($Sql);
            $doit = $STH->execute($vars);
            $this->Result = $doit;
            }
        catch(PDOException $e){
            echo $e->getMessage();
            }
        //find function to run
        switch($type){
            case 'select':
                $this->select($STH);
                break;
            }
        }

    public function select($query){
            $rows = $query->rowCount();
            $this->NumResults = $rows;
            while($row = $query->fetchObject()){
                $this->Response[] = $row;
            }
        }

    //create a separate function for connecting to DB. Private to only this class.
    private function db_connect(){
        $this->User = 'root';
        $this->Pass = '';
        $DBH = new PDO("mysql:host=localhost;dbname=divebaby", $this->User, $this->Pass);
        return $DBH;
        }
    }

?>

然后,要实际运行您放置在上面的语句,只需正确以下代码:

$username = $_POST['username'];
$password = sha1($_POST['password']);
$location = $_POST['location'];
$email = $_POST['email'];
$name = $_POST['fname'] . " " . $_POST['surname']; 


$getUser = new SqlIt("SELECT * FROM user WHERE username=?","select",array($username));

    if($getUser){ 
        echo 'Account name already exists';
    }else{
        $insertUser = new SqlIt("INSERT INTO user (username,password,location,email,name) VALUES (?,?,?,?,?)","insert",array($username,$password,$location,$email,$name));

       if($insertUser){ 
           echo 'Account created!';
       }else{
           echo 'Account not created.';
       }
于 2018-10-17T06:53:27.487 回答
1

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

于 2012-07-30T04:21:37.607 回答