[英]Insert cookie values with pdo
Hi I try to insert some cookie values with pdo into a Innodb on mysql. 嗨,我尝试将一些带有pdo的cookie值插入mysql的Innodb中。 The page before set a cookie with the userid. 设置用户ID的cookie之前的页面。 Now the problem is that no values become added in the database. 现在的问题是没有值添加到数据库中。 I think that the syntax at WHERE id = $userid
is wrong or the query syntax. 我认为WHERE id = $userid
处的语法错误或查询语法。 I get no error although I have catch(PDOException $e)
. 尽管我有catch(PDOException $e)
但没有任何错误。 How should I write it? 我应该怎么写?
try {
$dbh = new PDO("mysql:host=localhost;dbname=searchfood", $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
// prepare your query
$query = 'UPDATE users SET longitude=?, latitude=? WHERE id =?';
$stmt = $dbh->prepare($query);
// bind variables
$stmt->execute(array($_COOKIE['longitude'], $_COOKIE['latitude'], $userid));
// pull last insert id
$new = $dbh->lastInsertId();
}
Main problem here is that INSERT
query inserts NEW data to a table. 这里的主要问题是INSERT
查询将NEW数据插入到表中。 If you want to update existing record - use UPDATE
: 如果要更新现有记录,请使用UPDATE
:
UPDATE `table` SET field_name = "value" WHERE id = ID
Second problem is mixing placeholders for variables, you should use either ?
第二个问题是混合使用占位符作为变量,您应该使用其中一个?
or :name
in your queries (and never both) and bind variables accordingly. 或:name
在您的查询中( 绝不能同时使用),并相应地绑定变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.