如何在mysql表中添加PHP数组?
try {
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare the statement so that it gets repeated
$sql = $conn->prepare("INSERT INTO tasks (taskdescription) VALUE ('$task')");
//For each task in our list pass it into the query to be executed
foreach ($list as $task) {
$sql->execute($task);
}
echo "Tasks inserted into task table successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
我想在PDOPHP中执行此操作,以便它与我的其余代码匹配。目前它说它已插入任务,但当我检查phpmyadmin时它显示为空......
答案 0 :(得分:1)
如果您使用预备语句,则可以发送&#34;参数&#34;。 例如,您的任务(字符串类型?)可以作为查询中的参数发送:
$conn->prepare("INSERT INTO tasks (taskdescription) VALUE (:task)");
然后当你执行查询时:
$sql->execute(['task' => $task]);
答案 1 :(得分:-1)
首先,我建议使用准备好的语句并绑定它的参数。 试试这个:
$insertQuery = "INSERT INTO tasks (taskdescription) VALUE (:taskdescription)");
foreach ($list as $task) {
$stmt = $conn->prepare($insertQuery);
$stmt->bindParam(':taskdescription', $task);
$stmt->execute($task);
}