php pdo插入不';不归
我正在尝试使用pdo将表单插入数据库。每次我点击submit,它似乎根本没有通过我的功能。我觉得我好像忽略了什么(两个小时了) pdo连接工作(它在其他文件中工作) 我看不出我的错误。也没有任何警报得到回应 我觉得问题就在这里:php pdo插入不';不归,php,html,forms,Php,Html,Forms,我正在尝试使用pdo将表单插入数据库。每次我点击submit,它似乎根本没有通过我的功能。我觉得我好像忽略了什么(两个小时了) pdo连接工作(它在其他文件中工作) 我看不出我的错误。也没有任何警报得到回应 我觉得问题就在这里:if(isset($\u POST['insert']){ 或者用在我看来,它们似乎是正确的 我们将非常感谢您的帮助 <form action="insertdata.php" method="post"> <div id="mainwrapper"&
if(isset($\u POST['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>```
我复制了你的全部代码并运行了它。它运行得非常好
我认为您的表列数据类型有问题。请检查是否所有列都是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>
我的表单有一个操作,该操作会重新加载页面,而不是运行php函数。我只需删除它。谢谢您的帮助
<form method="post">
^
|
<form action="index.php" method="post">
^
|
如果使用if(isset($\u POST['location']){
会发生什么?这里也讨论了:@RobMoll注意到发生了有趣的事情。基本上是一样的。它是插入还是不回显“谢谢”/“出了问题”消息?@Edward它既不插入也不回显任何内容。感觉它所做的只是刷新页面。在$pdoQuery
行之前添加此内容:$pdoConnect->setAttribute(PDO::ATTR\u ERRMODE,PDO::ERRMODE\u异常)
OP指定的“pdo连接工作正常”
隐藏其实际参数,以免泄露其用户id/密码等。
<form method="post">
^
|
<form action="index.php" method="post">