[英]Why doesn't my MySQL update query work?
我正在創建博客編輯頁面,但是我的編輯頁面沒有任何作用。 為什么我的更新查詢不起作用? 我正在從一個舊博客中收集數據,並將其插入表單中。 然后,我嘗試使用更新查詢來更新它。
我認為這是您需要的代碼:
<?php
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)){
$error ='All fields are required!';
} else {
$query = $pdo->prepare("UPDATE articles SET article_title = ?, article_content = ? WHERE id=:id");
$id = $_POST ['id'];
$query->bindValue(1, $title);
$query->bindValue(2 ,$content);
$query->bindValue ('id', $id);
$query->execute();
header('Location: index.php');
}
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
$data = $article->fetch_data($id)
?>
<?php
} else {
header('Location: index.php');
exit();
}
?>
<form action="aanpassen.php" method="post" autocomplete="off">
<input type="" name="id" value="<?php echo $data['article_id']; ?>">
<input class="titleform" type="text" name="title" placeholder="Blog naam" value="<?php echo $data['article_title']; ?>" />
<textarea id="summernote" name="content" rows="15" cols="50">
<?php echo $data['article_content'] ?> </textarea>
<input class="buttonclass" type="submit" value="Aanmaken" /> </form>
您有一個“無效的參數號:混合的命名和位置參數”錯誤。
變化?
到占位符,然后更改為bindValue()
:
$query = $pdo->prepare("UPDATE articles SET article_title = :title,
article_content = :content WHERE id=:id");
$id = $_POST ['id'];
$query->bindValue('title', $title);
$query->bindValue('content', $content);
$query->bindValue('id', $id);
$query->execute();
或僅使用位置參數。
表單元素id
缺少type
屬性-可能默認為text
盡管可能不會引起錯誤,但是在預准備語句中混合占位符類型是不尋常的。 id
占位符在bindValue
調用中缺少冒號-再次可能還可以,盡管我認為應該始終在命名占位符中使用。
如果prepared statement
在初始階段失敗,則沒有邏輯可以對其進行測試。
<?php
$error=false;
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
if( $_SERVER['REQUEST_METHOD']=='POST' && $pdo ){
if ( isset( $_POST ['id'], $_POST['title'], $_POST['content'] ) ) {
$id = $_POST ['id'];
$title = $_POST['title'];
$content = nl2br( $_POST['content'] );
if ( empty( $title ) or empty( $content ) or empty( $id ) ){
$error='All fields are required!';
} else {
$query = $pdo->prepare("UPDATE articles SET article_title = :title, article_content = :content WHERE id=:id");
if( $query ){
$query->bindValue( ':title', $title );
$query->bindValue( ':content' ,$content );
$query->bindValue( ':id', $id );
$result=$query->execute();
header( sprintf( 'Location: index.php?status=%s', $result ? 'ok' : 'failed' ) );
} else {
exit('bad foo - unable to prepare sql query');
}
}
} else {
exit( sprintf( "<pre>check all required fields are named correctly\n\n%s</pre>", print_r( $_POST, true ) ) );
}
}
if ( isset( $_GET['id'] ) && $article ) {
$id = $_GET['id'];
$data = $article->fetch_data( $id );
} else {
header('Location: index.php');
exit();
}
?>
<form action="aanpassen.php" method="post" autocomplete="off">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="text" name="title" class="titleform" placeholder="Blog naam" value="<?php echo $data['article_title']; ?>" />
<textarea name="content" id="summernote" rows="15" cols="50"><?php echo $data['article_content'] ?></textarea>
<input type="submit" class="buttonclass" value="Aanmaken" />
</form>
<?php
if( $error )printf('<h1>%s</h1>',$error);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.