【发布时间】:2015-06-11 16:38:24
【问题描述】:
我尝试将 3 个 cookie 和带有 pdo 的日期插入到我的数据库中。但我不知道我该怎么做。输入值被插入到数据库中。并且cookies是在一个页面之前设置的。有什么提示吗?
<?php
$cookie_name = 'longitude';
if(isset($_POST["submit"])){
$hostname='localhost';
$user='root';
$password='';
$id = 'userid';
$_COOKIE["$id"];
$longitude = 'longitude';
$_COOKIE["$longitude"];
$latitude = 'latitude';
$_COOKIE["$latitude"];
$date = date('Y-m-d');
try {
$dbh = new PDO("mysql:host=$hostname;dbname=localy",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO post (title, text)
VALUES ('".$_POST["title"]."' ,'".$_POST["text"]."')";
if ($dbh->query($sql)) {
echo "New Record Inserted Successfully";
}
else{
echo "Data not successfully Inserted.";
}
$new = $dbh->lastInsertId();
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
if ($new > 0 && $_POST["title"] && $_POST["text"]) {
$message['success'] = 'Neuer Benutzer (' . htmlspecialchars($_POST['username']) . ') wurde angelegt, <a href="login.php">weiter zur Anmeldung</a>.';
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/loc/main.php');
}
else {
}
}
?>
【问题讨论】:
-
$_COOKIE["id"]=$id;像这样设置cookie -
即使您使用 PDO,为什么还要将数据直接放入 SQL 查询字符串中?!你那里有一些巨大的 sql 注入孔!请查看php.net/manual/en/pdo.prepared-statements.php#example-958 了解如何正确操作。