将数据插入 MySQL 后,我被重定向到 PHP 文件。不酷!如何在同一个 HTML 文件中显示错误或成功消息。
索引.html
<form id="form" action="add_article.php" method="post">
<div class="span4">
<div class="control-group">
<input type="text" placeholder="title" name="title"><br/><br/>
<input type="text" placeholder="author" name="author"><br/><br/>
<textarea placeholder="article summary" rows="12" maxlength="300" name="description"></textarea><br/><br/>
<input type="text" placeholder="location e.g. lubaga cathedral" name="location"><br/><br/>
</div>
</form>
.
add_article.php
<?php
$con=mysqli_connect("localhost","root","","bookdb");
if (!$con){
die ("Failed to connect to MySQL: " . mysqli_connect_error($con));
}
$sql = "INSERT INTO article (title, author, description,location)
VALUES('$_POST[title]','$_POST[author]','$_POST[description]','$_POST[location]')";
if(mysqli_query($con,$sql))
echo "Article inserted";
else
echo "An Error was Encountered";
mysqli_close($con);
?>