Php 为什么';我的MySQL更新查询不能工作吗?

Php 为什么';我的MySQL更新查询不能工作吗?,php,mysql,sql-update,Php,Mysql,Sql Update,我正在制作一个博客编辑页面,但我的编辑页面什么都不做。为什么我的更新查询不起作用?我正在从一个旧博客收集数据,并将其插入我的表单中。然后我尝试使用我的更新查询来更新它 我认为这就是您需要的代码: <?php include_once('includes/connection.php'); include_once('includes/article.php'); $article = new Article; if (isset($_POST['title'], $_POST['con

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

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

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


或者只使用位置参数。

表单元素
id
缺少
type
属性-可能默认为
text

虽然可能不会导致错误,但在准备好的语句中混合占位符类型是不常见的。
id
占位符在
bindValue
调用中缺少冒号-同样可能是可以的,但我认为它应该始终用于命名占位符中

如果
prepared语句
在初始阶段失败,则没有对其进行测试的逻辑

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


为什么我的更新查询不起作用?您面临任何错误?为什么要在sql中混合占位符?错误将是“$pdo”,您没有声明它。为什么您没有做任何事情来检查您的查询是否成功,如果没有,数据库为您提供了什么错误消息?现在,请不要问怎么做-这是你可以在任何地方阅读的东西,实际上在你来这里之前应该做的。没有为我做任何事吗?@Mr.Code你有没有尝试$pdo->setAttribute(pdo::ATTR_ERRMODE,pdo::ERRMODE_EXCEPTION)在您的
$pdo=new pdo
之后?它会给我一个“HTTP错误500”并重定向到我aanpassen.php代码在哪里失败您知道吗?它是否重定向到
?status=ok/failed
,或者甚至没有走那么远?我没有收到任何错误,只要单击submit按钮,它就会将我重定向到index.php,但什么都没有发生。
<?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);
?>