尝试使用以前的线程自己解决这个问题,但其他人的例子并没有帮助我弄清楚我需要什么。独立文件本身可以工作,数据很好地插入到数据库中......但我正在尝试将这些文档调整为一体化的单个PHP文档。
我想我会抛弃我正在尝试做的事情和我到目前为止的代码,也许有人会理解我想要完成的事情,并能够解释如何去做。
我基本上有一个带有form(form.php)的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>
提前感谢您提供的任何帮助。
答案 0 :(得分:0)
恢复您的form.php
,如下所示 -
我在这做了什么 -
1)<form action="insert.php" method="post">
要强>
<form action="" method="post">
2)在php code
form.php
<?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>";
}
?>
<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="" 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>
答案 1 :(得分:0)
我希望我明白这一点。在这种情况下,你就在附近。只需像这样合并你的脚本:
<?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>
我希望这接近你的期望。
答案 2 :(得分:0)
然后您的表单页面应该是这样的。
<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="" 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>
<?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>";
}
?>
感谢名单