创建更新表字段的管理页面


Creating an admin page that updates table fields

我正在尝试创建的是一个管理页面,用于编辑数据库中的表。此管理页面允许我在名为 index.php 的单独网页上编辑项目。我已经为管理页面设置了结构,但不知道从哪里开始对我的表进行更新。我真的需要一些关于如何实现这一目标的指导。就显示来自 tblContent 的数据而言,管理页面和索引.php当前工作正常。表中只有一行包含数据。表名为 tblContent,数据库名为 data1。我对此很陌生,所以我为我可怕的编码道歉。谢谢!

管理员.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>
<div id="container"> <!-- Open container -->
    <div id="header">
        <h1><a href="index.html"><?php
                                    require_once 'classes.php';
                                    $rh = $database->getPageHeading();
                                    print($rh);
                                    ?></a></h1>
        <h2>                     <?php
                                    require_once 'classes.php';
                                    $rs = $database->getSubHeading();
                                    print($rs);
                                    ?></h2>
    </div>
    <div id="content">
<form method="post" action="" onSubmit="return verifyPageHeading()">
<table width="300" cellpadding="2" cellspacing="2">
    <tr>
        <td>Page Heading:</td>
        <td><input type="text" id="txtPageHeading" name="txtPageHeading" value="<?php
                                                            require_once 'classes.php';
                                                            $rh = $database->getPageHeading();
                                                            print($rh);
                                                            ?>" class="inputClass"></td>
    </tr>
    <tr>
        <td>Sub Heading:</td>
        <td><input type="text" id="txtSubHeading" name="txtSubHeading" value="<?php
                                                                require_once 'classes.php';
                                                                $rs = $database->getSubHeading();
                                                                print($rs);
                                                                ?>"  class="inputClass"></td>
    </tr>
    <tr>
        <td>Page Title:</td>
        <td><input type="text" id="txtPageTitle" name="txtPageTitle" value="<?php
                                                                require_once 'classes.php';
                                                                $rt = $database->getPageTitle();
                                                                print($rt);
                                                                ?>"  class="inputClass"></td>
    </tr>
    <tr>
        <td>MetaDescription:</td>
        <td><textarea style="width:300px;"cols="55" rows="5" name="txtMetaDescription"><?php
                                                                                         require_once 'classes.php';
                                                        $rd = $database->getMetaDescription();
                                                        print($rd);
                                                        ?></textarea></td>
    </tr>
    <tr>
        <td>MetaKeywords:</td>
        <td><input type="text" name="txtMetaKeywords" value="<?php
                                                require_once 'classes.php';
                                                $rk = $database->getMetaKeywords();
                                                print($rk);
                                                ?>"  class="inputClass"></td>
    </tr>
    <tr>
        <td>Content:</td>
        <td><textarea id="textarea1" name="txtContent" style="height: 170px; width: 300px;" rows="15" cols="10"> <?php                                  
                                                                require_once 'classes.php';
                                                                $rc = $database->getContent();
                                                                print($rc);
                                                                ?></textarea>
        <input type="submit" value="Save" style="background-color:;border:1px solid ;color:;width:200px;"></td>
    </tr>

        </table>
            </form>
</div>  
<div id="footer">
</div>
</div> <!-- Close id="container" -->
</body>
</html>

类.php

<?php 
//Enter your database connection details here.
$host = 'localhost'; //HOST NAME.
$db_name = 'XXXXdata1'; //Database Name
$db_username = 'XXXXuser1'; //Database Username
$db_password = 'XXXXpass'; //Database Password
class database {
function __construct($pdo) {
    $this->pdo = $pdo;
}
function getPageHeading () {
    $query = $this->pdo->prepare('SELECT * FROM tblContent');
    $query->execute();
    return $query->fetchColumn(1);
}
function getSubHeading () {
    $query = $this->pdo->prepare('SELECT * FROM tblContent');
    $query->execute();
    return $query->fetchColumn(2);
}
function getContent () {
    $query = $this->pdo->prepare('SELECT * FROM tblContent');
    $query->execute();
    return $query->fetchColumn(3);
}
function getPageTitle () {
    $query = $this->pdo->prepare('SELECT * FROM tblContent');
    $query->execute();
    return $query->fetchColumn(4);
}
function getMetaDescription () {
    $query = $this->pdo->prepare('SELECT * FROM tblContent');
    $query->execute();
    return $query->fetchColumn(5);
}
function getMetaKeywords () {
    $query = $this->pdo->prepare('SELECT * FROM tblContent');
    $query->execute();
    return $query->fetchColumn(6);
}

}
try {
$pdo = new PDO('mysql:host='. $host .';dbname='.$db_name, $db_username, $db_password);
} catch (PDOException $e) {
exit('Error Connecting To DataBase');
}
$database = new Database($pdo);
?>
我想

通了,见下文。我使用 $_POST 从 admin.php 页面获取当前值,并使用 UPDATE 将现有表信息替换为在 admin.php 页面上输入的新信息。这更新了信息,这些信息将反映在我的索引.php页面上。我测试了它,它工作正常,但如果有更好的方法可以做到这一点,我是开放的。

<?php
//Enter your database connection details here.
$host = 'localhost'; //HOST NAME.
$db_name = 'XXXXdb_name'; //Database Name
$db_username = 'XXXXuser'; //Database Username
$db_password = 'XXXXpass'; //Database Password

$PageHeading = $_POST['txtPageHeading'];
$SubHeading = $_POST['txtSubHeading'];
$PageTitle = $_POST['txtPageTitle'];
$MetaDescription = $_POST['txtMetaDescription'];
$MetaKeywords = $_POST['txtMetaKeywords'];
$Content = $_POST['txtContent'];
try {
$conn = new PDO('mysql:host='. $host .';dbname='.$db_name, $db_username, $db_password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE tblContent SET PageHeading='$PageHeading', SubHeading='$SubHeading', 
PageTitle='$PageTitle', MetaDescription='$MetaDescription', MetaKeywords='$MetaKeywords', 
Content='$Content' WHERE PageID=1";
// Prepare statement
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>