【发布时间】:2019-08-07 22:26:17
【问题描述】:
我只想在 PHP 中接收来自 HTML 表单的输入。我做了将近 2 个小时的研究,尝试了我得到的每个代码,但它不起作用。这是我的代码:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Form site</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>
<form name="form1" method="post" action="connect.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3">Insert Data Into mySQL Database </td>
</tr>
<tr>
<td width="71">Name</td>
<td width="6">:</td>
<td width="301"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Lastname</td>
<td>:</td>
<td><input name="lastname" type="text" id="lastname"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table></body>
</html>
PHP:
<?php
$mysqli = new mysqli("localhost", "root", "", "test");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$tbl_name="employees"; // Table name
// Get values from form
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
// if successfully insert data into database, displays message "Successful".
$sql = "INSERT INTO $tbl_name (name, lastname, email) VALUES ('$name', '$lastname', '$email')";
$mysqli->query($sql);
?>
所以,当我在 Chrome 中运行 HTML 文件时,一切正常,但如果我随后填写表单并单击提交按钮,Chrome 会显示“此页面不起作用”。与数据库的连接肯定有效。我读到将 Apache 配置文件(使用 XAMPP)中的 AllowOverride 选项设置为“ALL”可能会有所帮助。它没有。 HTML 代码中指定的文件名与我实际使用的 PHP 文件的文件名相匹配。
PHP 是一种服务器端语言,不是吗?所以我怀疑我必须配置一些东西来运行 PHP 文件。我的计算机上已经安装了 PHP 我在命令行中使用 PHP 检查了数据库连接。显然我错过了一些东西。有人有想法吗?
提前致谢!
【问题讨论】:
-
<input name="name" type="text" id="name">!==$name = $_POST['firstname']; -
感谢并为我感到羞耻,但不幸的是它并没有解决我的问题
-
你应该在执行sql查询时检查错误。
-
@NiettheDarkAbsol 好的,这可能是原因。但是实现这一目标的解决方法是什么?
-
附带一个问题,您很容易受到 SQL 注入的攻击。你应该看看What is SQL injection?。