我在MYSQL& PHP所以不要介意我的被盗代码XD 我想通过使用PHP / HTML& amp;发布简单的帖子。节省数据库。
Ayway这里是我创建的文件,用于在home.php上发布内容
admin.php的
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dblogin";
$newsTitle = null;
$newsShortDescription = null;
$newsFullContent = null;
$newsColor = null;
$newsIcon = null;
$users_website = null;
$users_comment = null;
if(isset($_POST['btn-post']))
{
$newsTitle = $_POST['news_title'];
$newsShortDescription = $_POST['news_short_description'];
$newsFullContent = $_POST['news_full_content'];
$newsColor = $_POST['news_color'];
$newsIcon = $_POST['news_author'];
}
$newsTitle = mysql_real_escape_string($newsTitle);
$newsShortDescription = mysql_real_escape_string($newsShortDescription);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO info_news (news_title, news_short_description, news_full_content, news_author, news_color)
VALUES ('$newsTitle', '$newsShortDescription', '$newsFullContent', '$newsIcon', '$newsColor')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn = null;
?>
<form method="post">
<div class="form-group">
<input type="text" class="form-control" name="news_title" placeholder="Title" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_short_description" placeholder="Description" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_full_content" placeholder="Full Content" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_color" placeholder="Color" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_author" placeholder="Icon" required />
</div>
<div class="clearfix"></div>
<div class="form-group">
<button type="submit" name="btn-post" class="btn btn-block btn-primary">
Post
</button>
</div>
<br />
</form>
谢谢:)
我想制作一个简单的博客,只有标题,简短描述等等...它在我发布新帖子时有效,但是当我刷新它时显示一个空帖子。这是一张照片:Here is the Image我也尝试编辑admin.php(通过查看SOF上的其他问题)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dblogin";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("INSERT INTO info_news (news_title, news_short_description, news_full_content, news_author, news_color) VALUES (:news_title, :news_short_description, :news_full_content, :news_author, :news_color)");
$stmt->bindParam(':news_title', $news_title);
$stmt->bindParam(':news_short_description', $news_short_description);
$stmt->bindParam(':news_full_content', $news_full_content);
$stmt->bindParam(':news_author', $news_author);
$stmt->bindParam(':news_color', $news_color);
$stmt->execute($_POST);
?>
<form method="post">
<div class="form-group">
<input type="text" class="form-control" name="news_title" placeholder="Title" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_short_description" placeholder="Description" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_full_content" placeholder="Full Content" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_color" placeholder="Color" required />
</div>
<div class="form-group">
<input type="text" class="form-control" name="news_author" placeholder="Icon" required />
</div>
<div class="clearfix"></div>
<div class="form-group">
<button type="submit" name="btn-post" class="btn btn-block btn-primary">
Post
</button>
</div>
<br />
</form>
通过查看stackoverflow上的其他一些问题,但仍然没有运气; /
答案 0 :(得分:0)
这不是一个空帖,是什么使空行插入,试试这个:
if(isset($_POST['btn-post']))
{
$newsTitle = $_POST['news_title'];
$newsShortDescription = $_POST['news_short_description'];
$newsFullContent = $_POST['news_full_content'];
$newsColor = $_POST['news_color'];
$newsIcon = $_POST['news_author'];
$newsTitle = mysql_real_escape_string($newsTitle);
$newsShortDescription = mysql_real_escape_string($newsShortDescription);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO info_news (news_title, news_short_description, news_full_content, news_author, news_color)
VALUES ('$newsTitle', '$newsShortDescription', '$newsFullContent', '$newsIcon', '$newsColor')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
$conn = null;
}
我在$ conn = null之后移动了“}”; ;)
顺便说一句,你应该在每次POST后重定向。因此,在页面刷新时不再发送POST。
当内容已经“回显”时,您无法重定向。由于缓冲区,它会工作一段时间,但当标题的大小增加时(如4kB,取决于服务器设置),它将失败一天。 (在你发布的github上看过几次)