我的目标是创建一个论坛,其中包含一个选项,可以创建一个主题,其中详细信息、名称、电子邮件和日期时间是php要求的字段。create_topic.php是包含表单基本布局的文件。一旦按下submit,这些字段中的数据就会在post中被抓取。
一旦按下submit,action=“add\u topic.php”(下面的代码)。将处理我尝试插入的数据的文件。在脚本的底部有一个超链接,指向主题所在的位置。由于db中没有要显示的数据,所以我还没有看到实际的显示部分。只是暂时的。
有什么问题
执行代码时,postgres错误日志或vs代码中不会显示错误。正在尝试将数据插入postgresql。表仍为空。
我试过的
我尝试过将占位符的值从“:”改为“$”再改为“?”,但这些似乎都不管用。我已经通过在代码中引起有目的的停顿来确保存在实际的连接。pgsql log让我知道何时提供的参数不够,或者何时尝试将数据插入到不存在的表中。所以。。。我想这是有联系的。我可以插入数据从终端刚刚好。(我也有其他代码在我的主页,这是功能性的。)。我之所以提出这段代码,是因为它涉及到从postgresql中输入和检索,否则执行代码时,vs代码或日志中不会出现错误。谢谢朋友们的帮助。充其量是新手。只是想继续下一个挑战。愿原力与你同在
<?php
$tbl_name='forum_question';
require('forum_config.php');
try{
$db = new PDO("pgsql:dbname=$dbname;host=$dbhost", $dbuser, $dbpass) or die();
}catch(PDOException $e)
{
if(!$db){
print'no luck';
echo 'no luck';
}
echo-$e->getMessage();
}
$topic = $_POST['topic'];
$detail = $_POST['detail'];
$name = $_POST['name'];
$email = $_POST['email'];
$datetime=date("d/m/y h:i:s");
$inputdata = array ($topic, $detail, $name, $email, $datetime);
$query = $db->prepare('INSERT INTO forum_question(topic, detail, _name, email, _datetime) VALUES (:topic, :detail, :_name, :email, :_datetime)');
$query->bindParam(":topic",$topic);
$query->bindParam(":detail",$detail);
$query->bindParam(":_name",$name);
$query->bindParam(":email",$email);
$query->bindParam(":_datetime",$datetime);
$query->execute(array ($topic, $detail, $name, $email, $datetime));
$result = $query->fetchAll(PDO::FETCH_OBJ);
if($query){
echo "successful connection to DB $dbname<BR><br>";
echo "Below we have proof that POST data has been captured.
<br> The goal is to store in the TABLE $tbl_name which is inside the DATABASE $dbname.
<br> The captured data shall be displayed in desired forum.";
echo "<br>";
echo "<br>";
echo $result;
$query = $db->prepare("SELECT * from forum_question");
$query->execute();
$query->fetchAll(PDO::FETCH_OBJ);
echo "<br>";
echo "<br>";
echo $inputdata;
echo "<br>";
echo "<br>";
echo "<br>";
echo "topic: $topic";
echo "<br>";
echo "<br>";
echo "detail: $detail";
echo "<br>";
echo "<br>";
echo "name: $name";
echo "<br>";
echo "<br>";
echo "email: $email";
echo "<br>";
echo "<br>";
echo "<a href=main_forum_life.php>View your topic</a>";
echo "<br>";
echo"<br>";
}
else {
echo "<a href='error.php'>No biggie. Let us regroup.</a>";
die();
}
?>
/* Below is the SQL for the table that I am trying to insert $topic, $detail, $name, $email and $datetime into. */
创建表格论坛\u问题(
forum_question_uid SERIAL UNIQUE PRIMARY KEY,
topic VARCHAR(255) NOT NULL,
detail TEXT NOT NULL,
_name VARCHAR(65) NOT NULL,
email VARCHAR(65) NOT NULL,
_datetime VARCHAR(25) NOT NULL,
_view SERIAL,
reply SERIAL
);
2条答案
按热度按时间ercv8c1e1#
你有
AUTOCOMMIT
关掉了?请参阅[url]https://www.postgresql.org/docs/9.1/ecpg-sql-set-autocommit.html否则你需要做一个
commit
插入之后。你在这个案子里不会出错,因为没出什么差错。数据被插入,然后在连接结束时回滚。
lnvxswe22#
列数与缺少列“topic”的值数不匹配:
$pgquery='插入到论坛的\u问题(detail,\u name,email,\u datetime)值(:topic,:detail,:\u name,:email,:\u datetime)';