如何解决PHP-在数据库中插入实际的会话名称
因此,我正在使用PHP构建发布系统。当用户想要创建帖子时,所有字段都需要填写,而我想做的就是将会话的名称插入数据库,例如,插入数据库“ Edward”,因为那将是会话的名称。这是我想要做的:
<?php
session_set_cookie_params(86400*30,"/");
session_start();
require 'admin/config.php';
require 'functions.php';
if (isset($_SESSION['user'])) {
require 'view/new.view.php';
} else {
header('Location: index.php');
}
$connection = connect($bd_config);
if (!$connection) {
header('Location: error.php');
}
$errors = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = cleanData($_POST['title']);
$demo = cleanData($_POST['demo']);
@$project_type = $_POST['project_type'];
$content = $_POST['content'];
$post_by = $_SESSION['user'];
$errors = '';
if (empty($title) or empty($demo) or empty($project_type) or empty($content)) {
$errors .= '<p>Complete all the fields!</p>';
} else {
$statement = $connection->prepare("
INSERT INTO posts (ID,title,demo,content,post_type)
VALUES (null,:title,:demo,:content,:project_type)
");
$statement->execute(array(
':title' => $title,':demo' => $demo,':project_type' => $project_type,':content' => $content,));
$statement2 = $connection->prepare("
INSERT INTO posts (post_by)
VALUES ($post_by)
");
$statement2->execute(array(
$post_by
));
header('Location: main.php');
}
}
?>
如您所见,我正在为2个SQL咨询执行2个语句变量,但是当我这样做时,它将引发此错误:
<b>Fatal error</b>: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cesar' in 'field list' in C:\xampp\htdocs\bider\test2\new.php:52
Stack trace:
#0 C:\xampp\htdocs\bider\test2\new.php(52): PDOStatement->execute(Array)
#1 {main}
thrown in <b>C:\xampp\htdocs\bider\test2\new.php</b> on line <b>52</b><br />
我猜这是'cesar'原因,因为这是会话名称。 有人可以帮忙吗?
解决方法
因为这不是使用准备好的语句的方法。看第一个:
$statement = $connection->prepare("
INSERT INTO posts (ID,title,demo,content,post_type)
VALUES (null,:title,:demo,:content,:project_type)
");
您在这里告诉PHP如何通过预设名称(在两个点之后,例如:title或:content)发送数据
$statement->execute(array(
':title' => $title,':demo' => $demo,':project_type' => $project_type,':content' => $content,));
然后您可以将变量设置为那些预设名称并运行查询。
您正在尝试执行以下操作:
$statement2 = $connection->prepare("
INSERT INTO posts (post_by)
VALUES (:post_by)
");
然后您可以在数组中设置$ post_by,但是请记住,它是一个数组,$ post_by不是数组。看一下区别:
$statement2->execute(array(
':post_by' => $post_by
));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。