CRUD PDO在XAMPP上工作但在实时服务器上没有

时间:2018-01-18 19:16:48

标签: php mysql pdo

我正在尝试使用PDO更新帖子并将其添加到自定义编码博客中。

我可以阅读和删除,但添加帖子或编辑它们在实时服务器上不起作用。

它在XAMPP上工作正常...

我确保在我的数据库上启用了PDO_mysql扩展。

不知道问题在哪里?

我的编辑POST代码如下:

<?php

//if form has been submitted process it
if(isset($_POST['submit'])){

    //collect form data
    extract($_POST);

    //very basic validation
    if($postID ==''){
        $error[] = 'This post is missing a valid id!.';
    }

    if($postTitle ==''){
        $error[] = 'Please enter the title.';
    }

    if($postDesc ==''){
        $error[] = 'Please enter the description.';
    }

    if($postCont ==''){
        $error[] = 'Please enter the content.';
    }

    if(!isset($error)){

        try {

            $postSlug = slug($postTitle);

            //insert into database
            $stmt = $db->prepare('UPDATE blog_posts_seo SET postTitle = :postTitle, postSlug = :postSlug, postDesc = :postDesc, postCont = :postCont WHERE postID = :postID') ;
            $stmt->execute(array(
                ':postTitle' => $postTitle,
                ':postSlug' => $postSlug,
                ':postDesc' => $postDesc,
                ':postCont' => $postCont,
                ':postID' => $postID
            ));

            //delete all items with the current postID
            $stmt = $db->prepare('DELETE FROM blog_post_cats WHERE postID = :postID');
            $stmt->execute(array(':postID' => $postID));

            if(is_array($catID)){
                foreach($_POST['catID'] as $catID){
                    $stmt = $db->prepare('INSERT INTO blog_post_cats (postID,catID)VALUES(:postID,:catID)');
                    $stmt->execute(array(
                        ':postID' => $postID,
                        ':catID' => $catID
                    ));
                }
            }

            //redirect to index page
            header('Location: index.php?action=updated');
            exit;

        } catch(PDOException $e) {
            echo $e->getMessage();
        }

    }

}

?>


<?php
//check for any errors
if(isset($error)){
    foreach($error as $error){
        echo $error.'<br />';
    }
}

    try {

        $stmt = $db->prepare('SELECT postID, postTitle, postDesc, postCont FROM blog_posts_seo WHERE postID = :postID') ;
        $stmt->execute(array(':postID' => $_GET['id']));
        $row = $stmt->fetch(); 

    } catch(PDOException $e) {
        echo $e->getMessage();
    }

?>

我的ADD POST代码是

<?php

//if form has been submitted process it
if(isset($_POST['submit'])){

    //collect form data
    extract($_POST);

    //very basic validation
    if($postTitle ==''){
        $error[] = 'Please enter the title.';
    }

    if($postDesc ==''){
        $error[] = 'Please enter the description.';
    }

    if($postCont ==''){
        $error[] = 'Please enter the content.';
    }

    if(!isset($error)){

        try {

            $postSlug = slug($postTitle);

            //insert into database
            $stmt = $db->prepare('INSERT INTO blog_posts_seo (postTitle,postSlug,postDesc,postCont,postDate) VALUES (:postTitle, :postSlug, :postDesc, :postCont, :postDate)') ;
            $stmt->execute(array(
                ':postTitle' => $postTitle,
                ':postSlug' => $postSlug,
                ':postDesc' => $postDesc,
                ':postCont' => $postCont,
                ':postDate' => date('Y-m-d H:i:s')
            ));
            $postID = $db->lastInsertId();

            //redirect to index page
            header('Location: index.php?action=added');
            exit;

        } catch(PDOException $e) {
            echo $e->getMessage();
        }

    }

}

//check for any errors
if(isset($error)){
    foreach($error as $error){
        echo '<p class="error">'.$error.'</p>';
    }
}
?>

我是否应该检查MySQL数据库,或者我的代码中是否存在阻止我的CRUD在实时环境中工作的内容?

如前所述,完全相同的代码(配置文件除外)正在使用XAMPP。

2 个答案:

答案 0 :(得分:0)

可能是服务器端的权限错误,因为您无法编辑(写入)文件或创建新文件。我建议检查数据库的用户权限。

当你说它不起作用时,它又会给你带来什么? 您是否尝试过var_dump或echo如果它作为查询工作,或者它根本不允许查询执行?

<强>更新 最后但并非最不重要的是,评论建议的配置文件应该是主要问题。这实际上取决于您如何配置文件以在服务器上工作。检查包含等。

答案 1 :(得分:0)

问题是您如何插入DATE。 这是Insert, update and delete之间唯一的区别。

最好不要通过PHP添加它,因为你要添加当前时间,你可以让数据库为你做。

':postDate' => date('Y-m-d H:i:s')&lt; - 让db为你做默认参数设置为now()或者只使用TIMESTAMP可能是最好的。

相关问题