我已经使用以下命令在 Ubuntu 12.04 中安装了 lamp:
$ sudo apt-get install tasksel
和
$ sudo tasksel install lamp-server
并在浏览器中运行以下 .php 文件,并通过更新数据库来执行
<?php
$con=mysqli_connect("localhost","root","passwd","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");
mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire',33)");
mysqli_close($con);
?>
但是当我使用 .html 文件连接 .php 文件并更新 mysql 数据库时,.php 不会执行并想保存在文件夹中(默认为下载文件夹)。.html 文件是:
<html>
<body>
<form action="/var/www/EnterName.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
EnterName.php 文件是:
<?php
$con=mysqli_connect("localhost","root","passwd","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
请帮忙!
答案1
HTML 表单操作值必须是公共地址。因此,如果 html 文件和 EnterName.php 位于同一文件夹中,则只需使用 action="EnterName.php"