【问题标题】:Only Adding 1 Line to DB Then Skips the Rest只向数据库添加 1 行然后跳过其余的
【发布时间】:2012-11-22 13:30:28
【问题描述】:

我正在尝试将二维数组放入数据库。我的代码如下(这是PHP):

public function addspot($linearray,$data){
    $dbname=$data['dbname'];
    try {
        /* Create a connections with the supplied values */
        $pdo = new PDO("mysql:host=" . Config::read('hostname') . ";dbname=" . Config::read('database'). "", Config::read('username'), Config::read('password'), array(PDO::ATTR_PERSISTENT => true));
    } catch(PDOException $e) {
        /* If any errors echo the out and kill the script */
        return 'Database conncetion fail in assets/garage.class.php!Make sure your database information is correct';
    }

    foreach ($linearray as $lines) {
        $spot="INSERT INTO `$dbname`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('$lines[0]', '$lines[1]', '$lines[2]', '$lines[3]', '$lines[4]', CURRENT_TIMESTAMP);";
        $statement = $pdo->prepare($spot);
        if($statement->execute()){
            //silent
        } else {
            return 'Spot not added!';
        }
    }
}

配置值被正确读取,添加点的语句也是正确的。 我知道这一点是因为当我运行该函数时,它会正确添加 1 个“点”,但不会添加 2D 数组中的其余行。

我的数组如下:

array (size=16)
0 => 
array (size=5)
  0 => string '1' (length=1)
  1 => string '1' (length=1)
  2 => string '1' (length=1)
  3 => string '0' (length=1)
  4 => string '1' (length=1)
1 => 
array (size=5)
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => string '1' (length=1)
  3 => string '0' (length=1)
  4 => string '1' (length=1)
 (and onwards)

我的问题是我编写的函数只将第一行 (line[0]) 写入数据库,而其他的没有写入。

更新 语句的输出(使用 print_r): 准备后放置

PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '1', '1', '0', '1', CURRENT_TIMESTAMP);
)
PDOStatement Object
(
[queryString] => INSERT INTO `Garage2`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES ('1', '2', '1', '0', '1', CURRENT_TIMESTAMP);
)

print_r($pdo->errorInfo());输出 放在execute语句的else(fail)部分

Array
(
[0] => 00000
[1] => 
[2] => 
)

【问题讨论】:

  • font, small, b, i ?? 90 年代被调用并希望它的 html 回来 - 请学习 css 和现代 html
  • @Dagon Thats XDebug...对于那些在 21 世纪使用 PHP 的人:P 我会删除它的格式
  • 不使用绑定参数时,为什么要使用prepare?这也将摆脱正在发生的多个不需要的准备(虽然不是自动的)。注意PDO 不是可以用来替换mysql_* 代码的魔法。
  • @PeeHaa 如果没有,我会调用非对象上的成员函数 execute()
  • @PeeHaa 我不使用 mysql。从来没有。它已被弃用,所以我什至从未理会它。

标签: php mysql pdo


【解决方案1】:

终于明白了。 对于将来遇到此问题的人,请务必检查您的数据库中是否有主键,您没有两次使用相同的值。我的第一个单元格是我的主 ID,因此当第一个和第二个语句的值都为 1 时,它失败了。

为了找到这个,我在 prepare() 之后添加了这个\

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

【讨论】:

  • 那是桌子设计的味道。对于数字主 ID,最好使用自动增量。主 ID 必须是唯一的。但是,您可以使用不需要唯一的索引或组合索引。
  • @Tivie 我想要的是我知道第一列将包含数字 1-9,第二列也是如此。试图找到一种方法让数据库自动按最低的第一和最低的第二排序。
  • PrimaryID 不能用于此目的。您不能“对数据库进行排序”,但可以将查询获得的结果排序到数据库中。
【解决方案2】:

这应该使用准备好的语句来解决问题, 看看它是否有效并给我留言,然后我会更好地格式化答案

$spot="INSERT INTO `$dbname`(`floor`, `spot`, `status`, `uid`, `type`, `time`) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)";
$statement = $pdo->prepare($spot);
foreach ($linearray as $lines) {
    $values = array($lines[0],$lines[1],$lines[2],$lines[3],$lines[4])
    if($statement->execute($values)){
        //silent
    }
    else {
        return 'Spot not added!';
    }

【讨论】:

  • 已经解决(主要问题:见下文)。不过感谢您的帮助。
【解决方案3】:

对于数字主 ID,最好使用自动增量。主 ID 必须是唯一的。但是,您可以使用不需要唯一的索引或组合索引。

要回答有关排序结果的问题,请尝试以下查询:

-- Ordering by floor and spot
SELECT * FROM myTable ORDER BY floor, spot;

-- Ordering by floor only
SELECT * FROM myTable ORDER BY floor;

这是一个

SQLFiddle


注意:前面有 --(双破折号)的行是 cmets。您可以删除它们

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2021-02-08
    • 2019-06-14
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多