使用php pdo更新数据
我对使用PDO还不熟悉,所有这些都是用来添加和删除条目的,但我无法让它更新数据。我不确定我的sql语句是否已关闭,或者我只是缺少了一些东西使用php pdo更新数据,php,pdo,Php,Pdo,我对使用PDO还不熟悉,所有这些都是用来添加和删除条目的,但我无法让它更新数据。我不确定我的sql语句是否已关闭,或者我只是缺少了一些东西 if (isset($_GET['id'])) { $id = $_GET['id']; $data = $article->fetch_data($id); if(isset($_POST['title'], $_POST['content'])) { $title = $_POST['title'];
if (isset($_GET['id'])) {
$id = $_GET['id'];
$data = $article->fetch_data($id);
if(isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All fields are required!';
} else {
$query = $pdo->prepare('UPDATE articles SET article_title = ?, article_content = ?, article_timestamp = ? WHERE article_id = $id');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, time());
$query->execute();
header('Location: index.php');
}
}
}
您需要对查询进行双引号以插入$id变量。如果使用单引号,变量将被解释为$id
$query = $pdo->prepare("UPDATE articles SET article_title = ?, article_content = ?, article_timestamp = ? WHERE article_id = $id");
正如其他人所说,这可能导致SQL注入
相反,您还应该绑定$id变量:如果您使用int作为id,您可能还希望使用PDO::PARAM_int
。示例$query->bindValue(4,$id,PDO::PARAM_INT)代码>
您需要对查询进行双引号以插入$id变量。如果使用单引号,变量将被解释为$id
$query = $pdo->prepare("UPDATE articles SET article_title = ?, article_content = ?, article_timestamp = ? WHERE article_id = $id");
正如其他人所说,这可能导致SQL注入
相反,您还应该绑定$id变量:如果您使用int作为id,您可能还希望使用PDO::PARAM_int
。示例$query->bindValue(4,$id,PDO::PARAM_INT)代码>
我个人会这样写:
if (isset($_GET['id'])) {
$id = $_GET['id'];
$data = $article->fetch_data($id);
if(isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All fields are required!';
} else {
$sql = "UPDATE articles SET article_title = :title, article_content = :content, article_timestamp = :timestamp WHERE article_id = :id";
$query = $pdo->prepare($sql);
$query->bindValue(":title", $title);
$query->bindValue(":content", $content);
$query->bindValue(":timestamp", time());
$query->bindValue(":id", $id);
try {
$result = $query->execute();
} catch(PDOException $e) {
echo $e->getCode() . " - " . $e->getMessage();
}
if($result) {
header('Location: index.php');
}
}
}
}
这正是我喜欢的方式。如果您正在接受用户输入(我假设您来自$\u GET['id']
),那么您还应该传递id绑定,以帮助您进一步防止SQL注入。我学习PDO的方法也是通过将变量与“:”绑定。不能说我真的见过有人用这个吗?在处理PDO的时候,我更像是一个独行侠
不管怎样,这应该能满足你的需要
编辑:还有一件事我忘了提。添加$result=$query->execute()因为如果语句成功,PDO将返回true;如果语句不成功,PDO将返回false。因此,您可以重定向到另一个页面,或使用flash消息等重新显示表单。我个人会这样写:
if (isset($_GET['id'])) {
$id = $_GET['id'];
$data = $article->fetch_data($id);
if(isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All fields are required!';
} else {
$sql = "UPDATE articles SET article_title = :title, article_content = :content, article_timestamp = :timestamp WHERE article_id = :id";
$query = $pdo->prepare($sql);
$query->bindValue(":title", $title);
$query->bindValue(":content", $content);
$query->bindValue(":timestamp", time());
$query->bindValue(":id", $id);
try {
$result = $query->execute();
} catch(PDOException $e) {
echo $e->getCode() . " - " . $e->getMessage();
}
if($result) {
header('Location: index.php');
}
}
}
}
这正是我喜欢的方式。如果您正在接受用户输入(我假设您来自$\u GET['id']
),那么您还应该传递id绑定,以帮助您进一步防止SQL注入。我学习PDO的方法也是通过将变量与“:”绑定。不能说我真的见过有人用这个吗?在处理PDO的时候,我更像是一个独行侠
不管怎样,这应该能满足你的需要
编辑:还有一件事我忘了提。添加$result=$query->execute()因为如果语句成功,PDO将返回true;如果语句不成功,PDO将返回false。因此,您可以重定向到其他页面,或使用flash消息等重新显示表单。即使使用占位符,您也容易受到攻击$id
是用户直接提供的数据,您请求获取服务器pwn3d。即使使用占位符,您也容易受到攻击$id
是用户直接提供的数据,您请求获取服务器pwn3d。感谢您的建议!所以如果我把PDO::PARAM_INT;对于$id,我应该为time()之类的函数设置什么?我认为日期字段没有。下面是一些常量的列表:谢谢你的建议!所以如果我把PDO::PARAM_INT;对于$id,我应该为time()之类的函数设置什么?我认为日期字段没有。以下是一些常量的列表: