我有一个表单,可以帮助用户将值插入数据库。我在服务器端创建了一个检查,确保在数据库中没有输入重复的标题。一切正常。但是,我希望进行一些修改,我希望我在语句echo "title already exists";
中打印的消息应该在用户尝试提交表单时在标题框前面的表单中显示
<form class="form-horizontal" role="form" action="insert_project1.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-lg-3 control-label">Title</label>
<div class="col-lg-8">
<input class="form-control" name="title" value="" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Title</label>
<div class="col-lg-8">
<input class="form-control" name="title" value="" type="text">
</div>
</div>
</form>
insert_project1.php页面的代码
<?php
include('session.php');
$con=mysqli_connect("localhost","root","","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$title = mysqli_real_escape_string($con, $_POST['title']);
$categoryname = mysqli_real_escape_string($con, $_POST['categoryname']);
$goal = mysqli_real_escape_string($con, $_POST['goal']);
if($_POST)
{
$title = $_POST['title'];
$sql1 = "SELECT title from project";
$result = mysqli_query($con, $sql1);
if (mysqli_num_rows($result) > 0)
{
echo "title already exists";
}
else
{
$sql="INSERT INTO project (title, category, goal) VALUES ('$title', '$categoryname', '$goal')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: create_project.php");
exit;
}
}
mysqli_close($con);
?>
答案 0 :(得分:1)
试试这个:
session_start()
$con=mysqli_connect("localhost","root","","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$title = mysqli_real_escape_string($con, $_POST['title']);
$categoryname = mysqli_real_escape_string($con, $_POST['categoryname']);
$goal = mysqli_real_escape_string($con, $_POST['goal']);
if($_POST)
{
$title = $_POST['title'];
$sql1 = "SELECT title from project";
$result = mysqli_query($con, $sql1);
if (mysqli_num_rows($result) > 0)
{
$_SESSION['error_msg']='title already exists';
}
else
{
$sql="INSERT INTO project (title, category, goal) VALUES ('$title', '$categoryname', '$goal')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header("Location: create_project.php");
exit;
}
}
mysqli_close($con);
?>
<form class="form-horizontal" role="form" action="insert_project1.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-lg-3 control-label">Title</label>
<div class="col-lg-8">
<input class="form-control" name="title" value="" type="text">
<?php session_start(); if(isset($_SESSION['error_msg'])) echo $_SESSION['error_msg']; ?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Title</label>
<div class="col-lg-8">
<input class="form-control" name="title" value="" type="text">
</div>
</div>
</form>
答案 1 :(得分:0)
在你的HTML中:
<?php
if(isset($title_error)){
?>
<?php echo $title_error ?>
<?php } ?>
<label class="col-lg-3 control-label">Title</label>
在你的php文件中:
if (mysqli_num_rows($result) > 0)
{
$title_error = "title already exists";
}