From 2ffb770f8ab3007f59ed9141de14cd4fd9cda58e Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Wed, 19 Sep 2012 14:58:09 -0700 Subject: [PATCH] bugfix: don't fail when inserting multiple rows at once, and one of the values is NULL --- db.class.php | 2 +- simpletest/BasicTest.php | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/db.class.php b/db.class.php index 18cecea..b617e6e 100644 --- a/db.class.php +++ b/db.class.php @@ -355,7 +355,7 @@ class MeekroDB { $insert_values = array(); foreach ($keys as $key) { - if ($many && !isset($data[$key])) $this->nonSQLError('insert/replace many: each assoc array must have the same keys!'); + if ($many && !array_key_exists($key, $data)) $this->nonSQLError('insert/replace many: each assoc array must have the same keys!'); $datum = $data[$key]; $datum = $this->sanitize($datum); $insert_values[] = $datum; diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index 5c9879f..3fb9ab4 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -11,7 +11,7 @@ class BasicTest extends SimpleTest { DB::query("CREATE TABLE `accounts` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `username` VARCHAR( 255 ) NOT NULL , - `password` VARCHAR( 255 ) NOT NULL , + `password` VARCHAR( 255 ) NULL , `age` INT NOT NULL DEFAULT '10', `height` DOUBLE NOT NULL DEFAULT '10.0', `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi' @@ -195,9 +195,15 @@ class BasicTest extends SimpleTest { 'age' => 25, 'height' => 190.194 ); + $ins[] = array( + 'password' => NULL, + 'username' => '3ofmany', + 'age' => 15, + 'height' => 111.951 + ); DB::insert('accounts', $ins); - $this->assert(DB::affectedRows() === 2); + $this->assert(DB::affectedRows() === 3); $rows = DB::query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194); $this->assert(count($rows) === 2); @@ -207,6 +213,9 @@ class BasicTest extends SimpleTest { $this->assert($rows[1]['password'] === 'somethingelse'); $this->assert($rows[1]['username'] === '2ofmany'); + $nullrow = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", '3ofmany'); + $this->assert($nullrow['password'] === NULL); + $this->assert($nullrow['age'] === '15'); }