【问题标题】:A php progrme is called from a html file but .php file does not run and want to save从 html 文件调用 php 程序,但 .php 文件未运行并希望保存
【发布时间】:2013-06-05 11:12:18
【问题描述】:

我已经使用这些命令在 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/insert.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> 

insert.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);
?> 

请帮忙!

【问题讨论】:

  • 你是否验证过httpd.conf中安装了apache可以识别的php设置?
  • 首先,只尝试 php。只需写&lt;?php echo "testing php" ; ?&gt; 如果您在查看源代码中获得所有内容,则php 无法正常工作。 apache 正在运行吗?查看错误日志。有没有保存在www文件夹里面?如果你打了很久就放弃了,可以试试xampp等其他软件,但是lamp比xampp好,因为xampp适用于所有操作系统,lamp是linux独有的。请参阅help.ubuntu.com/community/ApacheMySQLPHP 并使用 google 获得更多帮助。如果您遇到特定错误,请复制该阶段并在 Google 上搜索。
  • @canadiancreed 文件,httpd.conf(目录/etc/apache2)包含AddType application/x-httpd-php .php
  • @web2students.com 。我试过 ' ' 和结果 testing php 在浏览器中。第二个 apache 正在运行('$ sudo /etc/init.d/apache2 restart')。三、/var/目录下已经存在WWW文件夹。

标签: php html lamp


【解决方案1】:
action="/var/www/insert.php"

你的路径看起来很奇怪,试试吧

action="insert.php"

【讨论】:

  • 是的,我已经尝试过 'action="insert.php"' 但出现了同样的问题。
【解决方案2】:

如前所述,表单中的路径/操作不正确。当您只需要相对于 URL 路径的路径时,您已经输入了文件的完整本地路径:

action="insert.php"

除此之外,您的 SQL 非常混乱,不能用于多字输入,并且容易受到注入。至少这样做:

$sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('".mysqli_real_escape_string($_POST["firstname"])."','".mysqli_real_escape_string($_POST[lastname])."','".mysqli_real_escape_string($_POST[age])."')";

或者更好的是,查看准备好的语句。

【讨论】:

  • 我按照你说的编辑了,' $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('".mysqli_real_escape_string($_POST["firstname"])."','" .mysqli_real_escape_string($_POST[lastname])."','".mysqli_real_escape_string($_POST[age])."')"; ' .但同样的想法也会发生。
【解决方案3】:

解决了! 我只是将html文件的一部分修改为,

<form action="http://localhost/insert.php" method="post">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    相关资源
    最近更新 更多