我正在尝试将$ _POST数据分配给字段,如果它与从SQL数据库中提取的$ row数据不同。这用于更新博客帖子。我的逻辑是,如果在文本字段中输入的内容与$ row数据不同,则将$ _POST数据分配给$ row数组。我不确定我的问题在哪里,因为它无法正常运行。任何帮助将不胜感激!
<?php session_start();
include('mysqli_connect.php');
$query = "SELECT * FROM blogposts WHERE blog_id=" . $_GET['id'];
$results = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($results, MYSQLI_ASSOC);
?>
<head>
<?php include('header.html'); ?>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
background-color:#fcfcfc;
}
form {
margin:auto;
}
</style>
</head>
<body>
<form name="com" id="com" action="<?php if (($_POST['blog_content'] != NULL)
&& ($_POST['blog_title'] != NULL)) {
echo "edit_handle.php";
} else {
echo "edit_post.php?id=" . $_GET['id'];
if(($_POST['blog_title'] != NULL) && ($row['title'] !=
$_POST['blog_title'])) {$row['title'] = $_POST['blog_title'];}
if(($_POST['blog_content'] != NULL) && ($row['content'] !=
$_POST['blog_content'])) {$row['content'] = $_POST['blog_content'];}
if(($_POST['blog_title'] != NULL) && ($_POST['blog_content'] != NULL)) {
$row['title'] = $_POST['blog_title'];
$row['content'] = $_POST['blog_content'];
}
} ?>" method="post">
<?php
if (isset($_SESSION['first_name']) && ($_SESSION['user_id'] == 11)) {
echo '
Blog Title: <input type="text" value="' . $row['title'] . '"
name="blog_title" />
Post Content:<textarea name="blog_content">' . $row['content'] .
'</textarea>
<input type="submit" value="submit">'; } else {
echo '<p align="center" style="color:red">You must be logged in as
<strong>admin</strong> to post a blog!</p>';
}
?>
</form>
<?php
if (($_POST['blog_content'] != NULL) && ($_POST['blog_title'] != NULL)) {
echo "<script>document.getElementById('com').submit();</script>";
}
?>
</body>
</html>
答案 0 :(得分:0)
我发现,处理混乱逻辑的最简单方法是制作人类可读的函数(或者如果你对OOP感到满意的类/方法)可以重复使用。如果您包含冗余脚本,您会发现更容易找出并隔离问题。这看起来像是一堆工作,但是如果你将函数移动到一个可包含的页面,你将大大清理视图:
<?php
# Fetch your post from the database
function getBlogPosts($dbc,$id)
{
# Check it's numeric
if(!is_numeric($id))
return false;
# Fetch and return
$results = mysqli_query($dbc, "SELECT * FROM blogposts WHERE blog_id=".$id);
return mysqli_fetch_array($results, MYSQLI_ASSOC);
}
# This function makes checking if it's set and if it has a value easy
function getPost($key = false)
{
if(!empty($key))
return (isset($_POST[$key]))? $_POST[$key] : false;
return $_POST;
}
# Same here
function getGet($key = false)
{
if(!empty($key))
return (isset($_GET[$key]))? $_GET[$key] : false;
return $_GET;
}
# Same here
function getSession($key = false)
{
if(!empty($key))
return (isset($_SESSION[$key]))? $_SESSION[$key] : false;
return $_SESSION;
}
# Isolate this for readability
function getActionByRequest()
{
# Set default
$default = "edit_handle.php";
# Check that there are post values
if(hasPostKeys())
return $default;
else
# Make sure the id is numeric, return default if not
return (is_numeric(getGet('id')))? "edit_post.php?id=".getGet('id') : $default;
}
# You seem to do this in a few places, so better to make it a function
function hasPostKeys()
{
return (!empty(getPost('blog_content')) && !empty(getPost('blog_title')));
}
# Make this easier to retrieve
function getDefaultValue($row,$key)
{
#Create dynamic key name
$blogKey = 'blog_'.$key;
# If both values are empty, stop
if(empty($row[$key]) && empty(getPost($blogKey)))
return false;
# If the post value doesn't match row, return post
return (getPost($blogKey) != $row[$key])? getPost($blogKey) : $row[$key];
}
# This is not the best way to check admin, but for this code it is fine
function isAdmin()
{
return (getSession('user_id') == 11);
}
session_start();
include('mysqli_connect.php');
# Get the row
$row = (!empty($_GET['id']))? getBlogPost($dbc,$_GET['id']) : array();
# Set this value based on post or by db return
# Doing it this way will ensure, these key/value pairs are always set
$row['title'] = getDefaultValue($row,'title');
# Set the default return for this key
$row['content'] = getDefaultValue($row,'content');
?><head>
<?php include('header.html'); ?>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
background-color:#fcfcfc;
}
form {
margin:auto;
}
</style>
</head>
<body>
<form name="com" id="com" action="<?php echo getActionByRequest() ?>" method="post">
<?php
# You really only have to use this function to check this
if (isAdmin()) { ?>
Blog Title: <input type="text" value="<?php echo $row['title'] ?>" name="blog_title" />
Post Content:<textarea name="blog_content"><?php echo $row['content'] ?></textarea>
<input type="submit" value="submit">
<?php } else { ?>
<p align="center" style="color:red">You must be logged in as <strong>admin</strong> to post a blog!</p>
<?php } ?>
</form>
<?php
if (hasPostKeys()) { ?>
<script>
document.getElementById('com').submit();
</script>
<?php } ?>
</body>
</html>