*我试图把一个本地数组值$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)
为了简洁起见,在声明title变量之前使用它。它应该在循环内部使用,像这样:$stmt->bindParam(':name', $i['name']);