mysql - PDO 准备好的语句插入数据库

标签 mysql prepared-statement

请问我有这个问题, 我这里有:

$query = $db->prepare('Update survey_content  set '.$type.' = '.$type.'+1  where id = '.$where);
$query->execute();

我是 PDO 新手,我真的不知道如何创建新查询,我需要在数据库中添加一条记录,这样可以吗?

$query1 = $db->prepare('INSERT INTO daily (selected)
VALUES (.$type.)');
$query1->execute();

最佳答案

使用占位符并在execute方法中填充值

$query1 = $db->prepare("INSERT INTO daily (selected)
                        VALUES (?)");
$query1->execute($selected_data);

或者单独绑定(bind)参数

$query1 = $db->prepare("INSERT INTO daily (selected)
                        VALUES (:sel_dat)");
$query1->bindParam(":sel_dat",$selected_data);
$query1->execute();

关于mysql - PDO 准备好的语句插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18161828/

相关文章:

java - 插入语句无法正常工作

sql - 使用 DDL 的准备语句

MySQL 报告 - 编辑器

php - mysql 数据库规范化

c# - 从表中获取结果,将它们存储在一个数组中,然后显示它们

mysql - SESSION group_concat_max_len 大小不够,更改大小时会给出不同的错误

Mysql查询、WP自定义字段

mysql - 我怎样才能让这种亲子关系发挥作用?

MySQL 使用预准备语句插入/更新包含点类型的行

php - 背靠背准备好的语句中的第二个准备好的语句存在问题