php - php pdo插入不返回
问题描述
我正在尝试使用 pdo 将表单插入数据库。每次我点击提交时,似乎它根本没有通过我的功能。我觉得我忽略了一些东西(现在 2 小时)。
pdo 连接有效(它在其他文件中有效)
我看不到我的错误。也没有任何警报得到回应。
我觉得问题出在这里:if(isset($_POST['insert'])){
或者<input name="insert">
在我看来,他们似乎是正确的。
一些帮助将不胜感激。
<form action="insertdata.php" method="post">
<div id="mainwrapper">
<input type="text" Placeholder="URL" name="url"><br>
<input type="text" Placeholder="Title" maxlength="40" name="title"><br>
<input type="textarea" Placeholder="Description" maxlength="200" name="description"><br>
<input type="text" Placeholder="Context" maxlength="25" name="location"><br>
<input id="submit" class="button" name="insert" type="submit" value="submit"></input>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// php insert data to mysql database using PDO
if(isset($_POST['insert'])){
echo '<script type="text/javascript">alert("test")</script>';
// connect to pdo
try{
$pdoConnect = new pdo('pdo connection works correctly');
}catch (PDOException $exc){
echo $exc->getMessage();
exit();
}
if(!$pdoConnect)
{
echo '<script type="text/javascript">alert("Oups")</script>';
}
// get values form input text and number
$url=$_POST['url'];
$title=$_POST['title'];
$description=$_POST['description'];
$location=$_POST['location'];
// mysql query to insert data
if($url=="")
{
echo '<script type="text/javascript">alert("You forgot something!")</script>';
}
$pdoQuery = "INSERT INTO `index` (`url`, `title`, `description`, `location`) VALUES (:url,:title,:description,:location)";
$pdoResult = $pdoConnect->prepare($pdoQuery);
$pdoExec = $pdoResult->execute(array(":url"=>$url,":title"=>$title,":description"=>$description,":location"=>$location));
// check if mysql insert query successful
if($pdoExec)
{
echo 'Thank you for submitting your website, you can now look for yourself';
}else{
echo 'Something went wrong, please contact us at a page we still need to make';
}
}
?>
</form>```
解决方案
我的表单有一个重新加载页面的操作,而不是通过 php 函数运行。我只需要删除它。谢谢您的帮助
<form method="post">
^
|
<form action="index.php" method="post">
推荐阅读
- mysql - IConfiguration.GetConnectionString MySql 连接池耗尽
- julia - 如何在 Julia 中测试特定数字有效数字的近似相等性
- python - 使用 HTTPBasicAuth 使用烧瓶创建自己的登录页面
- python - 正则表达式:用逗号编写要拆分的模式
- android - 使用匕首柄作为依赖注入来处理多个改造客户端?
- angular - 使用角度 12 安装 Plotly js 时出错
- elasticsearch - Elasticsearch 的亮点很慢
- python - 查找两个数据帧之间的差异
- html - 聚焦时如何更改兄弟元素的字体粗细?
- linux - 从两种不同方法构建的 libsqlite3.so.0.8.6 的大小