我要创建的是一个管理页面,用于编辑数据库中的表。此管理页面允许我在名为index.php的单独网页上编辑项目。我已经为管理页面设置了结构,但是对于从何处开始实现对表的更新,我很遗憾。我真的需要一些指导如何实现这一目标。管理页面以及index.php目前正在正常工作,只要显示来自tblContent的数据即可。表中只有一行包含数据。表名是tblContent,数据库是data1。我对此非常陌生,所以我为我可怕的编码道歉。谢谢!
admin.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>
classes.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);
?>
答案 0 :(得分:0)
我弄清楚了,见下文。我使用$ _POST从admin.php页面和UPDATE获取当前值,以使用admin.php页面上输入的新信息替换现有的表信息。这更新了将在我的index.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;
?>