尝试使用新消息在提交时插入变量

时间:2013-08-07 00:37:21

标签: php pdo

任何人都知道我的代码在下面做错了什么?我得到它来获取变量并显示主题,消息和日期很好,但我想要做的是让它从变量(来自textarea的新自定义消息)和日期从变量添加到主题数据库。

    <?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>

2 个答案:

答案 0 :(得分:0)

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

$query_params = array(
':username' => $_SESSION['user']['username'],
':subject' => $_POST['subject'],
':message' => $_POST['message'],
':date' => $_POST['date']
);

答案 1 :(得分:0)

date是一个MySQL关键字,您也忘记了:date。所以它应该是

$query = "
INSERT INTO supporttickets (
   username,
   subject,
   message,
   `date`
) VALUES (
   :username,
   :subject,
   :message,
   :date
)";