尝试使用以前的线程自己解决这个问题,但其他人的示例并不能完全帮助我弄清楚我需要什么。独立文件本身可以工作,数据可以很好地插入到数据库中...但我正在尝试对这些文档进行一些调整,以使其作为一个一体化的单个 PHP 文档工作。
我想我会扔掉我想要做的事情和到目前为止我拥有的代码,也许有人会理解我想要完成的事情并能够解释如何做到这一点。
我基本上有一个带有表单的 PHP 文档 (form.php),它使用 POST 将表单数据发送到第二个 PHP 文档,后者又将表单数据发送到 MySQL 数据库。我希望用户能够使用同一文档发布数据并重置数据字段,以便用户和提交通过表单输入更多信息。我希望让用户意识到由于字段不完整而导致的错误,并且我希望用户知道成功的条目(我将使用 echo 函数来做到这一点)。
form.php如下:
<html>
<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title>MySQL Database Creation System</title>
<script src="jquery-1.11.0.min.js"></script>
</head>
<body>
<div class="wrapper">
<img src="eris.png" align="middle">
<form action="insert.php" method="post">
Firstname: <input type="text" size="16" name="firstname">
Lastname: <input type="text" size="16" name="lastname">
Age: <input type="text" size="1" maxlength="3" name="age">
<input type="submit" class="button">
</form>
</div>
</body>
insert.php如下:
<html>
<head>
<link rel="stylesheet" type="text/css" href="insert.css">
<title>MySQL Database Creation System</title>
</head>
<body>
<div class="wrapper">
<img src='eris.png' align='middle'>
<?php
if ( (trim($_POST['firstname']) == "") || (trim($_POST['lastname']) == "") || (trim($_POST['age']) == "") )
{
echo "<p>ERROR: All fields must be completed</p>";
echo "<p><a href='form.php'>Return to Form</a></p>";
}
else
{
$con=mysqli_connect("localhost","test","test","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "<br>Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($_POST['firstname']);
$lastname = mysqli_real_escape_string($_POST['lastname']);
$age = mysqli_real_escape_string($_POST['age']);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('$firstname', '$lastname', '$age')";
if (!mysqli_query($con,$sql))
{
die('<br>Error: ' . mysqli_error($con));
}
mysqli_close($con);
echo "<p>New item added.</p>";
echo "<p><a href='form.php'>Return to Form</a></p>";
}
?>
</div>
</body>
预先感谢您提供的任何帮助。
最佳答案
我希望我明白了。在这种情况下,你就近了。只需合并您的脚本,如下所示:
<?php
// check if there is a post request ...
// if not - nothing happens
if(!empty($_POST))
{
if ( (trim($_POST['firstname']) == "") || (trim($_POST['lastname']) == "") || (trim($_POST['age']) == "") )
{
echo "<p>ERROR: All fields must be completed</p>";
echo "<p><a href='form.php'>Return to Form</a></p>";
}
else
{
// your inser code
$con=mysqli_connect("localhost","test","test","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "<br>Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($_POST['firstname']);
$lastname = mysqli_real_escape_string($_POST['lastname']);
$age = mysqli_real_escape_string($_POST['age']);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('$firstname', '$lastname', '$age')";
if (!mysqli_query($con,$sql))
{
die('<br>Error: ' . mysqli_error($con));
}
mysqli_close($con);
echo "<p>New item added.</p>";
echo "<p><a href='form.php'>Return to Form</a></p>";
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title>MySQL Database Creation System</title>
<script src="jquery-1.11.0.min.js"></script>
</head>
<body>
<div class="wrapper">
<img src="eris.png" align="middle">
<!-- change insert.php to form.php -->
<form action="form.php" method="post">
Firstname: <input type="text" size="16" name="firstname">
Lastname: <input type="text" size="16" name="lastname">
Age: <input type="text" size="1" maxlength="3" name="age">
<input type="submit" class="button">
</form>
</div>
我希望这接近您的期望。
关于php - 使用PHP POST方法发布到同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179809/