我正在尝试使用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>```
答案 0 :(得分:0)
“我的表单”执行的操作是重新加载页面,而不是通过php函数运行。我只需要删除它。谢谢您的帮助
<form method="post">
^
|
<form action="index.php" method="post">
答案 1 :(得分:-1)
我已经复制了您的整个代码,并运行了它。效果很好。
我认为您的表列数据类型有问题。检查是否所有列都为text或varchar数据类型。
这是您的代码:
<form action="index.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
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$pdoConnect = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
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';
}
?>
</div>
</form>
和表:
CREATE TABLE `index` (
`url` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;