使用 PHP 插入显示为下载而不是执行
抱歉,如果它已经存在,但我无法在任何地方找到它。
我正在尝试将表单中的信息插入名为“test”的数据库内名为“instest”的表中。 “instest”表内的列是“firstName”、“lastName”和“age”。
问题是,每次单击提交按钮时,它要么显示为下载(IE),要么显示代码(Opera),而不是实际执行的代码。
这是代码:
/* TestInsert.php */
<!DOCTYPE HTML>
<html>
<body>
<form action="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 = mysql_connect("localhost","root@localhost"); //No password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql="INSERT INTO instest (firstName, lastName, age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Sorry if this already exists, but I couldn't find this anywhere.
I am trying to insert information from a form into a table called "instest" inside a database called "test". The columns inside the "instest" table are "firstName", "lastName", and "age".
The problem is that every time click on the submit button it either shows up as a download (IE) or the code shows up (Opera), instead of the code actually executing.
Here is the code:
/* TestInsert.php */
<!DOCTYPE HTML>
<html>
<body>
<form action="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 = mysql_connect("localhost","root@localhost"); //No password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql="INSERT INTO instest (firstName, lastName, age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试添加
value="submit"
浏览器更喜欢正确的 htmlTry adding a
value="submit"
browsers prefer proper html看起来您只是忘记使用
或
打开
Insert.php
中的 PHP 标签(前者是最兼容,因此更可取的版本)。Looks like you simply forgot to open the PHP tags in
Insert.php
using either<?php
or<?
(the former being the most compatible, and therefore preferable, version).我认为你有一个错误:
尝试将其更改为:
我知道你可以在“”中包含变量(我个人认为这是不好的做法并且速度较慢)我认为你会遇到post var以及php如何解释的持续问题那。
您应该打开 error_reporting,因为它会反映这一点。
I think you have an error with this:
Try changing it to this:
I know you can have variables inside "" (I personally think its bad practice and slower) I think you will run into constant problems with the post var and how php interprets that.
You should turn on error_reporting as it would reflect that.