【问题标题】:Error with PDO inserting into database [closed]PDO插入数据库时​​出错[关闭]
【发布时间】:2014-06-19 22:03:55
【问题描述】:

我正在尝试创建一个简单的表单供用户输入他/她的姓名和消息。此表单还将有一个下拉菜单来选择此消息的收件人。所以我可以给菜单中的 5 个人中的任何一个写信息。我遇到的问题是它不会提交到数据库。我认为我的问题出在 php 文件中,但我不能完全确定它。我以前用过 PHP,但是 PDO 对我来说比较新,所以请多多包涵。

下面是我的html和php文件的代码。

HTML 表单文件:

<form name="gradMessage" method="POST" action="submitMessage.php">

    <label>Who would you like to send this message to?</label>

    <select name="person">
        <option name="nick" value="nick" class="dropdown">Nick</option>
        <option name="justin" value="justin" class="dropdown">Justin</option>
        <option name="liam" value="liam" class="dropdown">Liam</option>
        <option name="conner" value="conner" class="dropdown">Conner</option>
        <option name="kyle" value="kyle" class="dropdown">Kyle</option>
    </select><br>
    <br>
    <input type="text" id="name" name="name" title="Your name" 
        style="color:#888;" value="Your name" onfocus="inputFocus(this)"
        onblur="inputBlur(this)"><br>

    <textarea id="message" name="message" title="Message" 
        style="color:#888;" value="Message" onfocus="inputFocus(this)"
        onblur="inputBlur(this)" rows="5" cols="25">Your message</textarea><br>

    <input type="submit" name="submit" value="submit">
</form>

这是我的 PHP 文件 submitMessage.php:

<?php

    $to_data = $_POST['person'];
    $from_data = $_POST['name'];
    $message_data = $_POST['message'];
    $pic_path_data = "test";

    try {
        $user = xxxxxx;
        $pass = xxxxxx;

        $dbh = new PDO('mysql:host=xxxxxx;dbname=xxxxxxx', $user, $pass);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        # the data we want to insert
        $data = array( 'to' => $to_data, 'from' => $from_data, 'message' => $message_data );

        // I changed $DBH to $dbh and $STH to $sth and changed to to `to` and from to `from`
        # the shortcut!
        $sth = $dbh->("INSERT INTO message (`to`, `from`, `message`) value (:to, :from, :message)");
        $sth->execute($data);
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }

    echo 'hi there';

?>

<!DOCTYPE html>
<html>
    <head>
    </head>

    <body>
    <?php
        echo '$to_data';
    ?>
    </body>

</html>

【问题讨论】:

  • 你能发布错误信息吗?
  • Internal Server Error
  • 然后检查您的日志。
  • 这个echo '$to_data'; 需要用双引号括起来。 echo "$to_data";(旁注)

标签: php html mysql pdo


【解决方案1】:

More information on PDO prepared statements.

您缺少“准备”。

$sth = $dbh-&gt;("INSERT... 应该是$sth = $dbh-&gt;prepare("INSERT...

您可以从那里绑定准备好的语句中的变量。

使用命名占位符:

$sth = $dbh->prepare("INSERT INTO message (`to`, `from`, `message`) VALUE (:to, :from, :message)");
$sth->bindValue(':to', $to_data);
$sth->bindValue(':from', $from_data);
$sth->bindValue(':message', $message_data);
$sth->execute();

同样的想法使用? 占位符:

$sth = $dbh->prepare("INSERT INTO message (`to`, `from`, `message`) VALUE (?,?,?)");
$sth->bindValue(1, $to_data);
$sth->bindValue(2, $from_data);
$sth->bindValue(3, $message_data);
$sth->execute();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多