【发布时间】: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";(旁注)