-1
我想分配$ _POST數據到一個字段,如果它不同於從SQL數據庫拉出的$行數據。這是用來更新博客帖子。我的邏輯是,如果在文本字段中輸入的內容與$行數據不同,則將$ _POST數據分配給$行數組。我不確定我的問題在哪裏,因爲它無法正常工作。任何幫助將不勝感激!
<?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>
您是否有實際問題?你的問題是什麼? – Phil
警告:您的代碼易受[SQL注入攻擊](https://en.wikipedia.org/wiki/SQL_injection)的影響。請閱讀[本文](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)瞭解更多關於如何防止它。 – Pang
嘿@Phil對此抱歉,我不確定什麼是錯的或爲什麼它不工作! – user7928483