我正在尝试使用PHP数据对象执行基本的CRUD。我有两个文件:edit_list.php,在其中列出我拥有的页面,单击页面时,它将向用户发送带有单击页面ID的edit.php。在我的edit.php文件中,我运行一个查询来填充表单,如下所示:
if(isset($_GET['id'])){
$ID = $_GET['id'];
global $conn;
$query = ('SELECT * FROM pages WHERE page_id = :page_id');
$stmt = $conn->prepare($query);
$stmt->execute(array(':page_id' => $ID));
$selectPage = $stmt->fetch();`
我的表格如下所示:
<form action="edit.php" method="post">
<input style="width:500px;" type="text" name="title" placeholder="Page Title" value="<?php echo $selectPage['page_title']; ?>"/>
<input style="width:500px;" type="text" name="message" placeholder="Message" value="<?php echo $selectPage['page_message']; ?>"/>
<textarea rows="15" value="<?php echo $selectPage['page_content']; ?>" cols="60" placeholder="Content" name="content" style="margin-left: 0px; margin-right: 177px; width: 500px!important;"></textarea>
<input type='hidden' name='id' value='<?php echo $selectPage['page_id']; ?>' />
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
</form>
我刚在哪里跑步$selectPage["page_title"];
等等...
我正在尝试运行此查询:
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
try{
global $conn;
$updatequery = 'UPDATE pages SET page_title = :page_title, page_message = :page_message, page_content = :page_content WHERE page_id= :page_id';
$statement = $conn->prepare($updatequery);
$statement->bindValue(':page_title', $_POST['page_title']);
$statement->bindValue(':page_message', $_POST['page_message']);
$statement->bindValue(':page_content', $_POST['page_content']);
$statement->bindValue(':page_id', $_POST['page_id']);
$statement->execute();
header('Location:index.php');
}catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
}
它像执行了一样将我重定向回index.php,但是没有任何更新。我为此几乎撞墙了,绝望已取代。
您$_POST['page_message']
尚未将表单输入称为name="message"
,而不是匹配项。
也$_POST['page_title']
和input name="title"
不匹配。
您的方法POST
尚未if(isset($_GET['id'])){
与type='hidden' name='id'
这些都是会破坏您的代码的因素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句