config.php 是具有连接代码的全局文件。
我更新的 Insert.php 代码是:
<?php
include("config.php");
if (isset($_POST[firstname]) && isset($_POST[lastname]) && isset($_POST[age])) {
$sql = "INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
mysql_query($sql, $connect);
header("Location: add.htm");
// "1 record added";
}
else {
echo "No record added";
echo "<meta http-equiv=\"refresh\" content=\"5;url=add.htm\"/>";
}
if (!mysql_query($sql,$connect))
{
die('Error: ' . mysql_error());
}
mysql_close($connect);
?>
我的 View.php 代码是:
<?php
include("config.php");
$sql = mysql_query("SELECT * FROM Persons");
while($results = mysql_fetch_array($sql)) {
echo $results['FirstName'] . ', ' . $results['LastName'] . ', ' . $results['Age'] . '<br/>';
}
if (!mysql_query($sql,$connect))
{
die('Error: ' . mysql_error());
}
mysql_close($connect);
?>
我的 Config.php 代码是:
<?php
$dbhost="MYWEBHOST";
$dbusername="MYUSERNAME";
$dbpassword="MYPASSWORD";
$dbname="MYDATABASE";
$connect = mysqli_connect($dbhost, $dbusername, $dbpassword);
mysql_select_db($dbname,$connect) or die ("Could not connect to database");
?>
Insert.php 显示消息“添加了 1 条记录”并将用户重定向回 html 表单。哪个工作正常,但我不想在用户刷新或直接进入 Insert.php 时添加重复/空白值。如何在现有的 Insert.php 代码中完成此操作?