0

有人知道我在下面的代码做错了什么吗?我得到它来抓取变量并很好地显示主题、消息和日期,但我想做的是让它从变量中添加主题(来自文本区域的新自定义消息)和变量中的日期数据库。

    <?php
require("db.php");

$query_params = array(
  ':id' => $_GET['id'],
  ':username' => $_SESSION['user']['username']
);
$query = "SELECT id, subject, message, date FROM tickets
              WHERE username = :username And id= :id";
$stmt = $db->prepare($query);
  $result = $stmt->execute($query_params);
  $result = $stmt->setFetchMode(PDO::FETCH_NUM);
while ($row = $stmt->fetch($result)) {
    echo $row['subject'];
    echo $row['message'];
    echo $row['date'];

    PROBLEMS WITH THE CODE BELOW>>>>

if(!empty($_POST))
{
if(empty($_POST['message']))
{
$error="Please enter a Message.";
}
else
{
$query = "
INSERT INTO supporttickets (
username,
subject,
message,
date
) VALUES (
:username,
:subject,
:message,
date
)
";
$query_params = array(
':username' => $_SESSION['user']['username'],
':subject' => $_POST['subject'],
':date' => $_POST['date'],
':message' => $_POST['message']
);
   $stmt = $db->prepare($query);
   $result = $stmt->execute($query_params);
  }
 }
}
?>
<form action="" method="post" name="form">
<textarea name="message" cols="71" rows="9" id="message"></textarea>
<input type="submit" value="Submit" />
</form>
4

2 回答 2

0

尝试切换参数。它应该与您构建 INSERT 查询的方式相匹配。我交换了消息和日期。

$query_params = array(
':username' => $_SESSION['user']['username'],
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':date' => $_POST['date']
);
于 2013-08-07T00:41:02.743 回答
0

date是一个 MySQL 关键字,你也忘了放:date。所以应该是

$query = "
INSERT INTO supporttickets (
   username,
   subject,
   message,
   `date`
) VALUES (
   :username,
   :subject,
   :message,
   :date
)";
于 2013-08-07T00:48:01.303 回答