我有一个从dbs表行值派生的表,也为每行回显一个编辑按钮。单击编辑按钮,用户将指向特定表行的编辑表单,在我的案例事件中。这工作正常,用户登陆editevents.php页面,但我没有得到填充表单的数据。
if(isset($_POST['edit'])){
$eventid = $_POST['eventid'];
echo $eventid; *//echoing the eventid works fine so the parameter obvioulsy has been*
> passed through correctly via POST from the entry page*
$stmt = $event->runQuery("SELECT event_name, event_description, event_category, event_date, event_venue, event_location FROM events WHERE event_id=:$eventid");
$row=$stmt->fetch(PDO::FETCH_ASSOC);
表格:
<div id="updateevent">
<form action="" method="POST">
<label>Event title: </label>
<input type="text" name="eventname" value="<?php echo $row["event_name"]; ?>"/><br /><br />
<label>Description: </label>
<input type="text" name="eventdescription" required="required" value="<?php echo $row['event_description']; ?>"/><br /><br />
<label>Category: </label>
<input type="text" name="eventcategory" required="required" value="<?php echo $row['event_category']; ?>"/><br /><br />
<label>Venue: </label>
<input type="text" name="eventvenue" required="required" value="<?php echo $row['$event_venue']; ?>"/><br /><br />
<label>Event date: </label>
<input type="date" name="eventdate" required="required" value="<?php echo $row['event_date']; ?>"/><br /><br />
<label>Event location: </label>
<input type="text" name="eventlocation" required="required" value="<?php echo $row['$event_location']; ?>"/><br /><br />
<input type="submit" class="button" name="update" value="Update"
</form>
</div>
我没有收到错误消息,并且假设最有可能出现语法问题,因为我是绝对的初学者,有人可以给出建议吗?
更新
谢谢@Mike,我正在玩这个页面上的PHP,看它是如何工作的。我有这个干净的代码,它是在类事件文件中外包的实际函数:
public function displayevent($event_name,$event_description,$event_category,$event_date,$event_venue,$event_location)
{
try
{
$stmt = $this->conn->prepare("SELECT event_name, event_description, event_category, event_venue, event_location FROM events WHERE event_id=$eventid");
$stmt->bindparam('event_id'->$event_id);
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
如何在页面上调用此内容?
答案 0 :(得分:2)
您将命名参数与变量混合在一起。您也没有将变量绑定到查询。既然你没有使用原始的PDO,它显然会有所不同,但是如果你这样做,那就是你要做的。您只需要将其调整为$event
对象:
$stmt = $dbh->prepare(
"SELECT
event_name,
event_description,
event_category,
event_date,
event_venue,
event_location
FROM
events
WHERE
event_id=:eventid"
);
$stmt->execute(['eventid' => $eventid]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
注意:建议缩短长线。