我想用變量插入到多個表中。當我硬編碼特定的表名稱時,它運行正常,當我使用一個變量時,我得到一個QUERY FAILEDSQLSTATE [42000]:語法錯誤或訪問衝突:1064錯誤。 dbname是變量。我正在使用for循環來更改表的名稱。例如表1是budget1000,則預算2000等。這裏是我的代碼使用php變量將值插入到mysql表中
$sql='INSERT INTO ".$dbName." VALUES(:id,:category,:subCategory,
:amount, :today,:description, :year)';
try{
$st= $conn->prepare($sql);
$st->bindValue(":id", $id, PDO::PARAM_INT);
$st->bindValue(":category", $category, PDO::PARAM_INT);
$st->bindValue(":subCategory", $subCategory, PDO::PARAM_INT);
$st->bindValue(":amount", $amount, PDO::PARAM_INT);
$st->bindValue(":today", $today, PDO::PARAM_STR);
$st->bindValue(":description", $description, PDO::PARAM_STR);
$st->bindValue(":year", $year, PDO::PARAM_INT);
$st->execute();
}catch(PDOException $e){
echo "QUERY FAILED" . $e->getMessage();
}
你應該只使用一個表,而不是一堆 –