English 中文(简体)
PHP中PHP中PDO编制的说明,使用连带阵列生成错误结果
原标题:PDO prepared statement in PHP using associative arrays yields wrong results

我正在开发一个非常小的 PHP 和 MySQL 应用程序。 我有一个叫做用户的分类, 用于操作数据库中的用户数据, 数据库中包含一个创建用户方法, 如下所示 :

/**
* Creates a new user record in the users table for a new user
*   
* @return void
*/
public function createUser($user_id, $user_name, $location_id) {

    // query to execute
    $query = "INSERT INTO 
                      users(user_id,user_name,location_id)
                      VALUES(:id,:name,:location)";

    // query parameters
    $parameters = array (
         :id  => $user_id,
         :name  => $user_name,
         :location  => $location_id
    );

    $databaseInteractions = new DatabaseInteractions();
    $databaseInteractions->executeUpdate($this->_connection, $query, $parameters);

}

我还有另一类,其中将包含与数据库互动的通用方法,如上述代码所示,数据库称为数据库互动。本类包含一种方法,即执行 DML 查询的更新日期,如下文所示:

/**
 * A function for executing DML queries using a prepared statement
 * @param $connection The database connection object
 * @param $query The dml query to be executed
 * @param $parameters The input parameters for the query as a hash 
 */
public function executeUpdate($connection,$query, $parameters) {

    if ($stmt = $connection->prepare($query)) {

        //bind query parameters
        foreach ($parameters as $key => $value) {
            $stmt->bindParam($key, $value);
        }
        //begin transaction
        $connection->beginTransaction();

        $stmt->execute();
        $stmt->closeCursor();

        //commit transaction
        $connection->commit();
    }
}    

当我在下面显示的用户类中调用创建用户方法时,

$user->createUser(3,"NewUser",1);

数据库中插入的数值如下:-

   user_id   user_name  location_id
    1              1             1

我花了一些时间调试代码, 但仍然无法找到发生这种情况的原因 。 用户_ id 是 PK, 是一个intt。 用户_ name is varchar, 位置_ id is FK and int 。

我是爪哇开发商,是PHP的新开发商,因此,与php命名惯例、编码标准等有关的任何投入总是受欢迎的。

最佳回答
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签