<?php
if (isset($_POST["Submit"])) {
$Category = mysqli_real_escape_string($Connection, $_POST["Category"]);
date_default_timezone_set("Asia/Tokyo");
$CurrentTime = time();
$DateTime = strftime("%d-%Y %H:%M:%S", $CurrentTime);
$DateTime;
$Admin = "XYZ";
if (empty($Category)) {
$_SESSION["ErrorMessage"] = "All Fields must be filled out!";
Redirect_to("Categories.php");
} elseif (strlen($Category) > 99) {
$_SESSION["ErrorMessage"] = "Too long name!";
Redirect_to("Categories.php");
} else {
//the issue occurs below this...
global $ConnectingDB; //$ConnectingDB selects the database
$Query = "INSERT INTO category(datetime,name,creatorname)
VALUES('$DateTime','$Category','$Admin')";
$Execute = mysqli_query($Connection, $Query); //$Connection is the link
if ($Execute) {
$_SESSION["SuccessMessage"] = "Categories Added Successfully!";
Redirect_to("Categories.php");
} else {
$_SESSION["ErrorMessage"] = "Category failed to add!";
Redirect_to("Categories.php");
}
}
}
?>
//the form code...
<form action="Categories.php" method="POST">
<fieldset>
<div class="form-group">
<label for="categoryname">Name:</label>
<input type="text" class="form-control" name="Category" id="categoryname"
placeholder="Name">
</div>
<input class="btn btn-default" type="Submit" name="Submit"
value="Add New Category">
</fieldset>
</form>
问题:当我输入类别名称并将其提交到数据库时,该查询未在我的数据库(phpmyadmin)中写入新条目。该页面上显示的警报消息是“类别添加失败”。我应该如何解决这个问题?我只有4列:ID,日期时间,名称,创建者名称。
db.php:
<?php
$Connection = mysqli_connect('localhost', 'root', '');
if (!$Connection) {
die("Database connection failed: " . mysqli_connect_error());
}
$ConnectingDB = mysqli_select_db($Connection, 'myblog');
if (!$ConnectingDB) {
die("Database selection failed: " . mysqli_connect_error());
}
$ _SESSION消息:
<?php
function Message () {
if (isset($_SESSION["ErrorMessage"])) {
$Output="<div class=\"alert alert-danger\">";
$Output.=htmlentities($_SESSION["ErrorMessage"]);
$Output.="</div>";
$_SESSION["ErrorMessage"]=null;
return $Output;
}
}
function SuccessMessage () {
if (isset($_SESSION["SuccessMessage"])) {
$Output="<div class=\"alert alert-success\">";
$Output.=htmlentities($_SESSION["SuccessMessage"]);
$Output.="</div>";
$_SESSION["SuccessMessage"]=null;
return $Output;
}
}
?>
//this is to display the success message in managing categories
<div><?php echo Message(); echo SuccessMessage(); ?></div>
我是PHP的初学者。