dongxingqiu7943 2017-03-29 10:26
浏览 20

更新使用PDO与mysql失败

insert select and delete are all working but my update is failing. I first select a single record into a form, the values from that record are populating the form, so far so good. However when I send the form I get the following error:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Blog::updateBlogPost(), 0 passed in /Applications/XAMPP/xamppfiles/htdocs/TravelClub/members/editPost.php on line 91 and exactly 2 expected in /Applications/XAMPP/xamppfiles/htdocs/TravelClub/Blog.Class.php:124 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/TravelClub/members/editPost.php(91): Blog->updateBlogPost() #1 /Applications/XAMPP/xamppfiles/htdocs/TravelClub/members/editPost.php(25): processForm() #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/TravelClub/Blog.Class.php on line 124

The PDO statement in my blog class is as follows:

// update record from form data
    public function updateBlogPost($postid, $body)
    {
        $conn = parent::connect();
        $sql = "UPDATE blog set body = :body where postid = :postid";
        echo $sql;
        try {
            $st = $conn->prepare($sql);
            $st->bindValue(":postid", $postid, PDO::PARAM_STR);
            $st->bindValue(":body", $body, PDO::PARAM_STR);
            $st->execute();
            parent::disconnect($conn);
        } catch (PDOException $e) {
            parent::disconnect($conn);
            die("Query failed: " . $e->getMessage());
        }
    }

and the php script which creates the form and handles the script is:

<?php
require_once( "../common.inc.php" );



checkLogin();
// show nav bar for members area
displayNavBar();

$postid = isset( $_GET["postid"] ) ? (string)$_GET["postid"] : 0;
$body = isset( $_GET["body"]) ? (string)$_GET["body"] :0;
//$post = Blog::viewBlogPost($postid );
//$body = Blog::viewBlogPost($body );

echo "<br>";
echo "the post id is " .$postid;
echo "<br>";
echo "the blog text is " . $body;


// if the form action is register add user or handle errors
if ( isset( $_POST["action"] ) and $_POST["action"] == "updateBlogPost" ) {
    $postid = isset( $_GET["postid"] ) ? (int)$_GET["postid"] : 0;
    $body = isset( $_GET["body"] ) ? (int)$_GET["body"] : 0;
    processForm();
} else {
    displayForm( array(), array(), new Blog( array() ) );
}


// if there are no errors display the form
function displayForm( $errorMessages, $missingFields, $blog ) {
    displayPageHeader( "Edit your blog post" );

    if ( $errorMessages ) {
        foreach ( $errorMessages as $errorMessage ) {
            echo $errorMessage;
        }
    } else {
        ?>
    <?php } ?>


    <form action="editPost.php" method="post" style="margin-bottom: 50px;">
        <div style="width: 30em;">
            <input name="action" type="hidden" value="updateBlogPost">
            <!--<label for="userid"<?php validateField( "postid", $missingFields ) ?>>user id</label>-->
            <input type="hidden" id="postid" cols="50" name="postid" rows="4" value="<?php echo $blog->getValueEncoded( "postid" ) ?>; ?>"><?php echo $blog->getValueEncoded( "postid" ) ?>
            <input type="hidden" id="body" cols="50" name="body" rows="4" value="<?php echo $blog->getValueEncoded( "body" ) ?>; ?>"><?php echo $blog->getValueEncoded( "body" ) ?>
           <input type="text" value="<?php echo htmlspecialchars($_GET['postid']); ?>">
            <div style="clear: both;">
                <label for="body"<?php validateField( "body", $missingFields ) ?>><?php echo $blog->getValueEncoded( "body" ) ?></label>

                <textarea id="body" cols="50" name="body" rows="4" value="<?php echo htmlspecialchars($_GET['body']); ?>"><?php echo htmlspecialchars($_GET['body']); ?></textarea>
                <div style="clear: both;">
                    <input id="submitButton" name="submitButton" type="submit" value="Send Details">
                    <input id="resetButton" name="resetButton" style="margin-right: 20px;" type="reset" value="Reset Form">
                </div>
            </div>
    </form>
    <?php
    displayPageFooter();
}

//  process the form data and store it in the database
function processForm() {


    $requiredFields = array( "postid", "body" );
    $missingFields = array();
    $errorMessages = array();
    $blog = new blog( array(
        "postid" => isset( $_POST["postid"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]/", "", $_POST["postid"] ) : "",
        "body" => isset( $_POST["body"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]/", "", $_POST["body"] ) : "",
        "postdate" =>  date( "Y-m-d H:i:s" )

    ) );
    foreach ( $requiredFields as $requiredField ) {
        if ( !$blog->getValue( $requiredField ) ) {
            $missingFields[] = $requiredField;
        }
    }
    // deal with errors
    if ( $missingFields ) {
        $errorMessages[] = '<p class="error">There were some missing fields in the form you submitted. Please complete the fields highlighted below and click Send Details to resend the form.</p>';
    }

    if ( $errorMessages ) {
        displayForm( $errorMessages, $missingFields, $blog );
    } else {
        $blog->updateBlogPost();
        displayThanks();
    }
}

// when the values are stored, display a thankyou page
function displayThanks() {

    displayPageHeader( "Your post has been updated" );
    ?>
    <p>Thank you, your blog post has been updated.</p>

    <a href="blog.php">go to blog page</a>
    <?php
    displayPageFooter();
}
?>

Ive tried adding the two varibles to this line:

$blog->updateBlogPost();

i.e.

$blog->updateBlogPost($postid, $body);

it then says these are undefined, even if I add the declarations to that particular routine the record doesn't update.

I've been stuck on this for a couple of days now and any help would be greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch