Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分配$\u POST数据以覆盖现有数组值_Php_Mysql_Arrays_Variables - Fatal编程技术网

Php 分配$\u POST数据以覆盖现有数组值

Php 分配$\u POST数据以覆盖现有数组值,php,mysql,arrays,variables,Php,Mysql,Arrays,Variables,如果某个字段与从SQL数据库提取的$row数据不同,我将尝试将$u POST数据分配给该字段。这是用来更新博客帖子的。我的逻辑是,如果在文本字段中输入的内容与$row数据不同,则将$\u POST数据分配给$row数组。我不确定我的问题在哪里,因为它不能正常工作。任何帮助都将不胜感激 <?php session_start(); include('mysqli_connect.php'); $query = "SELECT * FROM blogposts WHERE blog_id="

如果某个字段与从SQL数据库提取的$row数据不同,我将尝试将$u POST数据分配给该字段。这是用来更新博客帖子的。我的逻辑是,如果在文本字段中输入的内容与$row数据不同,则将$\u 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>

表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
文本对齐:居中;
背景色:#fcfc;
}
形式{
保证金:自动;
}

我发现,处理混乱逻辑的最简单方法是使人类可读的函数(或类/方法,如果您熟悉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>

您有实际问题吗?您的问题是什么?警告:您的代码易受攻击。请阅读了解更多关于如何预防它的信息。嘿@Phil抱歉,我不确定是什么问题或者为什么它不起作用!Hey@Pang谢谢你提供的信息,但别担心,这是一个学校项目,不会公开提供。如果你在两个数组中的密钥名称相同,你可以使用
array\u filter
过滤数组以去除
NULL
s,然后使用
array\u merge
array\u intersect
的组合。