簡體   English   中英

使用PDO和HTML更新SQL表

[英]Updating sql table with pdo and html

我創建了CMS,以便管理員用戶可以更輕松地對網站進行更改,而無需編輯代碼。

因此,基本上,管理員用戶登錄網站后,便應該能夠添加,刪除和編輯內容。 我已經設法使添加和刪除功能正常工作,但是我在編輯部分上很掙扎。

當用戶決定編輯時,他們將被定向到顯示存儲在數據庫中的每篇文章的頁面,然后他們可以單擊想要編輯的任何文章。 數據通過函數$articles = $article->fetch_all();從數據庫中$articles = $article->fetch_all(); 在新頁面上,腳本將使用$data = $article->fetch_data($id);從mysql表中獲取數據$data = $article->fetch_data($id); 這可以正常工作,並且可以按我想要的方式獲取數據。 但是,當我進行任何更改並單擊“更新內容”按鈕時,我僅被重定向到空白頁,並且未保存任何更改。 我試圖查找錯誤,但未顯示任何錯誤,也看不到我要去哪里。 Article類是存儲上述功能的位置。

我嘗試了幾種不同的更新方法,但實際上沒有一種工作,而且我看不到哪里出錯了。

這是管理員決定編輯時首先看到的頁面:

<?php

session_start();

include_once('../includes/connection.php');
include_once('../includes/article.php');

$article = new Article;
$articles = $article->fetch_all();


if (isset($_SESSION['logged_in'])) {

?>



<html>
    <head>
    <title>Select the content you want to edit</title>
    <link rel="stylesheet" href="../assets/style.css"/>
    </head>

    <body>
        <div class="container">
        <a href="index.php" id="logo">CMS</a>

        <br /><br />

        <h4>Select the content you want to edit</h4>

<table>
    <thead>
        <tr>
            <th>Article ID</th>
            <th>Artilce Title</th>
            <th>Article Content</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($articles as $article) : ?>
        <tr>
            <td><?php echo ($article["article_id"]); ?></td>
            <td><?php echo ($article["article_title"]); ?></td>
            <td><?php echo ($article["article_content"]); ?></td>
            <td><a href="update.php?id=<?php echo $article['article_id'];?>">Edit</a></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

</html>

<a href="index.php">Back to home</a>

    <?php

}else{
    header('Location: index.php');
}

?>

管理員單擊一個進行編輯后,它們將被定向到update.php頁面:

<?php

session_start();

include_once('../includes/connection.php');
include_once('../includes/article.php');

$article = new Article;

if (isset($_SESSION['logged_in'])) {
    if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $data = $article->fetch_data($id);
    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{
        $sql = "UPDATE articles SET article_title = :title, 
            article_content = :article_content 
            WHERE article_id = :article_id";
            $stmt = $pdo->prepare($sql);                                  
            $stmt->bindParam(':article_title', $_POST['title'], PDO::PARAM_STR);       
            $stmt->bindParam(':article_content', nl2br($_POST['$content']), PDO::PARAM_STR);       
            $stmt->bindParam(':article_id', $_POST['article_id'], PDO::PARAM_INT);   
            $stmt->execute(); 

        header('Location: index.php');

            }

        }


    ?>
    <html>
    <head>
    <title>CMS Tutorial</title>
    <link rel="stylesheet" href="../assets/style.css"/>
    </head>

    <body>
        <div class="container">
        <a href="index.php" id="logo">CMS</a>

        <br />

        <h4>Update Article</h4>


        <?php if (isset($error)) { ?>

        <small style="color:#aa0000"><?php echo $error; ?></small> 
        <br /><br />

        <?php } ?> 

        <form action ="update.php" method="post"  autocomplete="off">
        <input type="text" value ="<?php echo $data['article_title']; ?>"name="title"/required><br /><br />
        <textarea  rows="15" cols="50" required name="content"> <?php echo $data['article_content']; ?></textarea><br /><br />
         <input type="submit" value="Update Content"/>




        </form>
    </div>  
    </body>
<html>

<?php

    }   

}else{
    header('Location: index.php');
}

?>

查看教程,看來我做得正確,但是每次都將我重定向到空白頁面,並且沒有任何更新。

編輯:弄亂了一點之后,問題似乎出在我獲取數據的方式上:

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $data = $article->fetch_data($id);

我的fetch_data函數的工作方式如下:

公共功能fetch_data($ article_id){全局$ pdo;

$query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
$query->bindValue(1, $article_id);
$query->execute();

return $query->fetch();

}

我更改了添加的sql,因為我知道我可以使它正常工作,但是它做了同樣的事情(在提交表單時進入空白頁),直到我擺脫了獲取數據的方式。 所以我知道這一定與它有關,但我一輩子都無法弄清楚

我在哪里出錯呢?

以前,我建議未定義$ pdo。 您評論的情況並非如此,您是正確的,因此我進行了詳細介紹。

主要原因是,當您發布表單時,“ article_id”未提交。 表格中僅有的兩個字段是“標題”和“內容”。 查詢字符串中的“ id =”不會神奇地包含在您的表單中。 因此,讓我們將ID作為隱藏輸入添加到表單中:

<input type="hidden" name="id" value="<?php echo $data['article_id']; ?>">

我們還沒有到那里-因為僅在設置了$ _GET ['id']的情況下才處理表單,但是這次我們使用POST。 $ _REQUEST ['id']將解決問題,它將處理$ _GET ['id']和$ _POST ['id'],因此請替換這兩行:

if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];

在您的PDO語句中,用id替換article_id,並且由於您已經分配了$ title和$ content,我們也來清理它:

$stmt->bindParam(':article_title', $title, PDO::PARAM_STR);       
$stmt->bindParam(':article_content', $content, PDO::PARAM_STR);  
$stmt->bindParam(':article_id', $id, PDO::PARAM_INT);   

因此,在經過Jeroen Flamman和Jacques Barker(非常感謝您的幫助!)提供的非常有幫助的答案之后,我終於通過以下方法使它正常工作:

<?php

session_start();

include_once('../includes/connection.php');
include_once('../includes/article.php');

$article = new Article;

if (isset($_SESSION['logged_in'])) {
    if (isset($_REQUEST['id'])) {
    $id = $_REQUEST['id'];
    $data = $article->fetch_data($id);
    if (isset($_POST['title'], $_POST['content'])) {
        $title = $_POST['title'];
        $content = nl2br($_POST['content']);

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

        //collect form data
        extract($_POST);

        if($title ==''){
            $error[] = 'This post is missing a valid id!.';
        }

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


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

        if(!isset($error)){

            try {

                //insert into database
                $stmt = $pdo->prepare('UPDATE articles SET article_title = :title, article_content = :content WHERE article_id = :id') ;
                $stmt->execute(array(
                    ':title' => $title,
                    ':content' => $content,
                    ':id' => $id
                ));

                //redirect to index page
                header('Location: index.php');
                exit;

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

        }

    }
        }

    }


    ?>
    <html>
    <head>
    <title>CMS Tutorial</title>
    <link rel="stylesheet" href="../assets/style.css"/>
    </head>

    <body>
        <div class="container">
        <a href="index.php" id="logo">CMS</a>

        <br />

        <h4>Update Article</h4>


        <?php if (isset($error)) { ?>

        <small style="color:#aa0000"><?php echo $error; ?></small> 
        <br /><br />

        <?php } ?> 

        <form action ="update.php" method="post"  autocomplete="off">
        <input type="text" value ="<?php echo $data['article_title']; ?>"name="title"/required><br /><br />
        <textarea  rows="15" cols="50" required name="content"> <?php echo $data['article_content']; ?></textarea><br /><br />
        <input type="hidden" name="id" value="<?php echo $data['article_id']; ?>">
         <input type="submit" name="submit" value="Update Content"/>




        </form>
    </div>  
    </body>
<html>

<?php



}else{
    header('Location: index.php');
}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM