0

*我正在尝试将本地数组值 $productname 放入 sqlite3 表中,该数组如下所示:

[0] usb 16gb
[1] monitor 16"
[2] dual batteries
[3] multi blender pro

等等*

// Prepare INSERT statement into sqlite3
  $insert = "INSERT INTO products (name) 
            VALUES (:name)";

  $stmt = $db->prepare($insert);

  // Bind parameters to statement variables
  $stmt->bindParam(':name', $title);

  // Loop all product title and execute
   foreach ($productname as $i)
  {
        // Set values to bound variables
        $title = $i['name'];
        // Execute statement
         $stmt->execute();
  }

  $result= $db->query('SELECT * FROM products');
    foreach($result as $row) 
     {
        echo "Id: " . $row['id'] . "\n";
        echo "Title: " . $row['name'] . "\n";
     }

虽然这确实有效,但它会打印出来

Id: 1
Title: U
Id: 2 
Title: M
Id:3 
Title: D
Id:4 
Title: M 

我哪里错了?它不会打印出整个字符串(产品名称)......?它还会自我迭代大约 80 次,而实际上只有 4 个左右的标题。基本上我希望将一个 php 数组放入 sqlite3 表列(productTitle)

4

1 回答 1

0

简而言之,title 变量在声明之前使用。它应该在循环内部使用,如下所示:$stmt->bindParam(':name', $i['name']);

于 2013-06-04T15:00:44.423 回答