首页 > 解决方案 > 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>```

标签: phphtmlforms

解决方案


我的表单有一个重新加载页面的操作,而不是通过 php 函数运行。我只需要删除它。谢谢您的帮助

<form method="post">
^
|
<form action="index.php" method="post">


推荐阅读