PHP pdo插入查询不起作用

时间:2016-05-22 01:29:28

标签: php mysql pdo

<?php
// DATABASE-HOSTNAME-OR-IPADDRESS-GOES-HERE
// MYSQL-DBNAME-GOES-HERE
class LoginHandler {
    public $dbHostname = 'localhost';
    public $dbDatabaseName = 'employee101';
    public $user = 'root';
    public $password = 'root';
    public function handleRequest($arg) {
        $username = '123';
        $password2 = '123';
        $fname = 'John';
        $lname = 'Doe';
        $age = '18';
        if ( ! $username ) {
            $this->fail();
            return;
        }
        try  {
            $dsn = "mysql:dbname={$this->dbDatabaseName};host={$this->dbHostname};port=8888";
            $pdo = new PDO($dsn, $this->user, $this->password);
            $sql="SELECT * FROM `employee_data` WHERE `username`='$username'";
            $stmt = $pdo->query($sql);
            if ( $stmt === false ) {
                echo "DB Critical Error";
                return;
            }
            elseif ( $stmt->rowCount() > 0 ) {
                echo "user already exists";
                return;
            }
            else {
                echo "User created";
                $sql = "INSERT INTO employee_data (name, sumame, age, username, password)
                VALUES ($fname, $lname, $age, $username, $password2)";
                $dsn = "mysql:dbname={$this->dbDatabaseName};host={$this->dbHostname};port=8888";

                $pdo = new PDO($dsn, $this->user, $this->password);
                $stmtz = $pdo->prepare($sql);

                $stmtz->bindParam($fname, $_POST[$fname], PDO::PARAM_STR);
                $stmtz->bindParam($lname, $_POST[$lname], PDO::PARAM_STR);
                $stmtz->bindParam($age, $_POST[$age], PDO::PARAM_STR);
                $stmtz->bindParam($username, $_POST[$username], PDO::PARAM_STR);
                $stmtz->bindParam($password2, $_POST[$password2], PDO::PARAM_STR);

                $resultzzx = $stmtz->execute();
                return;
            }
        }
        catch(PDOException $e) {
            $this->log('Connection failed: ' . $e->getMessage());
            echo "DB Critical Error";
        }
    }
    function log($msg) {
        file_put_contents("login.log", strftime('%Y-%m-%d %T ') . "$msg\n", FILE_APPEND);
    }
}
$handler = new LoginHandler();
$handler->handleRequest($_POST);
?>

当尝试使用上面的脚本时,我得到用户创建的回显,但即使刷新表,新条目也不会显示。

现在,如果我将值行更改为以下内容,它将起作用并显示新条目。

('John', 'Doe', '18', $username, $password2)";

我做错了什么?我需要名字,姓氏和年龄条目不具体,因为我将从我的Android设备上的POST获取它们。此脚本的整个目的是创建用户及其记录(如果它尚不存在)。

2 个答案:

答案 0 :(得分:2)

你有各种各样的错误。

1)您没有正确绑定参数。要正确绑定它们,请将:variablename放在要包含变量的位置。通常是&#34;变量名&#34;应该与您从$_POST超全局获得的那个相同,以便代码更清晰,更易读。

2)您没有正确地从$_POST超全球获取值。您放置的key值是字符串,通过放置一个空的$fname变量,您将无法获得正确的结果。它只会在你从我们这里隐藏起来的某个地方编写$fname = 'fname'编码时才会起作用,但是这个代码本身不会受到修改,因为它是不必要的,只会使源代码更大。

$sql = "INSERT INTO employee_data (name, sumame, age, username, password)
VALUES (:fname, :lname, :age, :username, :password2)";

$dsn = "mysql:dbname={$this->dbDatabaseName};host=
{$this>dbHostname};port=8888";

$pdo = new PDO($dsn, $this->user, $this->password);

$stmtz = $pdo->prepare($sql);

$stmtz->bindParam(':fname', $_POST['fname']);
$stmtz->bindParam(':lname', $_POST['lname']);
$stmtz->bindParam(':age', $_POST['age']);
$stmtz->bindParam(':username', $_POST['username']);
$stmtz->bindParam(':password2', $_POST['password2']);

我希望有所帮助。

答案 1 :(得分:1)

 $sql = "INSERT INTO employee_data (name, sumame, age, username, password) VALUES (:name, :sumame, :age, :username, :password)";
                $dsn = "mysql:dbname={$this->dbDatabaseName};host={$this->dbHostname};port=8888";

                $pdo = new PDO($dsn, $this->user, $this->password);
                $stmtz = $pdo->prepare($sql);
                $stmtz->bindParam(':name', $fname);
                $stmtz->bindParam(':sumame', $lname);
                $stmtz->bindParam(':age', $age);
                $stmtz->bindParam(':username', $username);
                $stmtz->bindParam(':password', $password2);
                $resultzzx = $stmtz->execute();
                return;

在查看上面评论中发布的Fred链接后,我已将其修改为正常工作,谢谢。