简体   繁体   English

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

[英]Why doesn't my MySQL update query work?

I'm making a blog edit page, but my edit page doesn't do anything. 我正在创建博客编辑页面,但是我的编辑页面没有任何作用。 Why doesn't my update query work? 为什么我的更新查询不起作用? I'm collecting the data from an old blog and inserting it into my form. 我正在从一个旧博客中收集数据,并将其插入表单中。 And then I'm trying to update it using my update query. 然后,我尝试使用更新查询来更新它。

I think this is the code you need: 我认为这是您需要的代码:

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

You have a "Invalid parameter number: mixed named and positional parameters" error. 您有一个“无效的参数号:混合的命名和位置参数”错误。

Change ? 变化? to placeholders, and change to bindValue() : 到占位符,然后更改为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();

Or use only positional parameters. 或仅使用位置参数。

The form element id was missing a type attribute - probably defaulted to text 表单元素id缺少type属性-可能默认为text

Whilst probably not going to cause errors the mixing of placeholder types in the prepared statement is unusual. 尽管可能不会引起错误,但是在预准备语句中混合占位符类型是不寻常的。 The id placeholder was missing the colon in the bindValue call - again possibly OK though to my mind it should always be used in named placeholders. id占位符在bindValue调用中缺少冒号-再次可能还可以,尽管我认为应该始终在命名占位符中使用。

If the prepared statement failed the initial stage there was no logic to test for it. 如果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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM