为什么我的MySQL更新查询不起作用?

时间:2018-03-12 08:21:44

标签: php mysql sql-update

我正在制作博客编辑页面,但我的编辑页面没有做任何事情。为什么我的更新查询不起作用?我正在从旧博客收集数据并将其插入到我的表单中。然后我尝试使用我的更新查询来更新它。

我认为这是您需要的代码:

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

2 个答案:

答案 0 :(得分:1)

您有一个&#34;无效的参数编号:混合命名和位置参数&#34; 错误。

?更改为占位符,然后更改为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();

或仅使用位置参数。

答案 1 :(得分:0)

表单元素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);
?>