PHP插入数据失败

时间:2017-03-22 16:16:46

标签: php mysql

PHP

if(isset($_POST['insert'])) {
    try {
        $pdoConnect = new PDO("mysql:host=localhost:3307;dbname=project","root","usbw");
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }

    $issue = $_POST['issue'];
    $content = $_POST['content'];
    $contact = $_POST['contact'];

    $pdoQuery = "INSERT INTO `checks`(`issue`, `content`, `contact`) VALUES (:issue,:content,:contact)";

    $pdoResult = $pdoConnect->prepare($pdoQuery);

    $pdoExec = $pdoResult->execute(array(":issue"=>$issue,":content"=>$content,":contact"=>$contact));

    if($pdoExec) {
        echo 'Data Inserted';
    } else {
        echo 'Data Not Inserted';
    }
}

HTML

<form action="insert.php" method="post">
    <input type="text" name="issue" required placeholder="issue"><br><br>
    <input type="text" name="content" required placeholder="content"><br><br>
    <input type="text" name="contact" required placeholder="contact"><br><br>
    <input type="submit" name="insert" value="Insert Data">
</form>

我没有插入唱片,有什么事吗?我是编码的新手,有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

为什么不保持简单,减少代码?

<?php

// error reporting enabled
error_reporting(1);
ini_set('error_reporting', E_ALL);

// databse connection
$pdoConnect = new PDO("mysql:host=localhost;dbname=project","root","usbw");

if(isset($_POST['insert']))
{
    $issue = $_POST['issue'];
    $content = $_POST['content'];
    $contact = $_POST['contact'];

    $stmt = $pdoConnect->prepare("INSERT INTO checks ( issue, content, contact ) VALUES (:issue, :content, :contact)");
    $stmt = $pdoConnect->execute(array(":issue"=>$issue, ":content"=>$content, ":contact"=>$contact));

    if($pdoConnect->lastInsertId())
    {
        echo 'Data Inserted';
    }
    else
    {
        echo 'Data Not Inserted';
    }
}

?>

答案 1 :(得分:0)

O()

以下是使用pdo进行连接的方法   if(isset($_POST['insert'])) { try { $pdoConnect = new PDO("mysql:host=localhost;dbname=project","root","usbw");// take off the port number you provided } catch (PDOException $exc) { echo $exc->getMessage(); exit(); } $issue = $_POST['issue']; $content = $_POST['content']; $contact = $_POST['contact']; $pdoQuery = "INSERT INTO `checks`(`issue`, `content`, `contact`) VALUES (:issue,:content,:contact)"; $pdoResult = $pdoConnect->prepare($pdoQuery); $pdoExec = $pdoResult->execute(array(":issue"=>$issue,":content"=>$content,":contact"=>$contact)); if($pdoExec) { echo 'Data Inserted'; } else { echo 'Data Not Inserted'; } } 从php文档中,没有为localhost提供端口号;  我测试了你的代码,现在它运行正常。database entries PDO doc是here
希望它有所帮助。