【发布时间】:2016-01-13 07:08:21
【问题描述】:
所以我正在尝试创建一个 html 表单,该表单使用 PHP 将数据发布到一个充满 MySQL 客户的数据库,但对 PHP 来说还是很陌生。
目前,每当我尝试使用提交按钮“在此服务器上找不到请求的 URL /bankyprinting/post”提交所有内容时,都会收到 404。数据也没有注入 MySQL 数据库,但我没有收到任何其他错误,表明没有建立与数据库的连接。
我正在尝试编写/使用的应用程序是:
customers.html
<html>
<head>
</head>
<body>
<form method = "post" action = "customers.php" id="customers">
First Name:
<input type = "text" name = "FirstName"/><br>
LastName:
<input type = "text" name = "LastName"/><br>
Company:
<input type = "text" name = "Company"/><br>
Position:
<input type = "text" name = "Position"/><br>
Address:
<input type = "text" name = "Address"/><br>
Phone Number:
<input type = "text" name = "PhoneNumber"/><br>
Cell Number:
<input type = "text" name = "CellNumber"/><br>
Alternate Number:
<input type = "text" name = "AlternateNumber"/><br>
E-Mail:
<input type = "text" name = "EMail"/><br>
<input type = "submit" name="submit" value = "submit"/><br>
</form>
</body>
<footer>
</footer>
</html>
index.html
</html>
<head>
</head>
<body>
<a href = "customers.html">New Customer</a>
</body>
<footer>
</footer>
</html>
connect.php
<?php
$host="localhost";
$port=3306;
$socket="/tmp/mysql.sock";
$user="root";
$password="";
$dbname="bankyprinting";
$con = mysqli_connect($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
//$con->close();
?>
和 客户.php
<?php
/*Needs the connection object created in the connect.php file to work*/
header("LOCATION:customers.html");
require('connect.php');
/*require('customers.html');*/
/*Data from the html form is on the right. The objects that will be composed of that data is on the left.*/
if(isset($_POST['submit'])) {
$Company = mysqli_real_escape_string($con, $_POST['Company']);
echo 'Company';
$Position = mysqli_real_escape_string($con, $_POST['Position']);
echo 'Position';
$FirstName= mysqli_real_escape_string($con, $_POST['FirstName']);
echo 'FirstName';
$LastName = mysqli_real_escape_string($con, $_POST['LastName']);
echo 'LastName';
$Address = mysqli_real_escape_string($con, $_POST['Address']);
echo 'Address';
$PhoneNumber = mysqli_real_escape_string($con, $_POST['PhoneNumber']);
echo 'PhoneNumber';
$CellNumber = mysqli_real_escape_string($con, $_POST['CellNumber']);
echo 'CellNumber';
$AlternateNumber = mysqli_real_escape_string($con, $_POST['AlternateNumber']);
echo 'AlternateNumber';
$EMail = mysqli_real_escape_string($con, $_POST['Email']);
echo 'EMail';
$sql = "INSERT INTO tblcustomers (Company, Position, FirstName, LastName, Address, PhoneNumber, CellNumber, AlternateNumber, EMail)
VALUES ('$Customer', '$Position', '$FirstName', '$LastName', '$Address', '$PhoneNumber', '$CellNumber', '$AlternateNumber', '$EMail')";
if ($con->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
}
?>
我已将所有这些存储在 WAMP 服务器托管的文件夹中——是的,我已经知道 html 表单不安全——但这超出了我现在的问题范围 ><.>
我不知道为什么会出现 PHP 错误(POST 错误?),并且不知道如何通过让表单正确注入数据库来解决这个问题。
【问题讨论】:
-
它们都在同一个文件夹中吗?这四个文件?
-
用引号将变量括起来
''例如'$Customer' -
你不能像在 bankyprinting.html 中那样在 html 中包含 php 文件
-
Company!=$Company(所有其他变量分配和用法也是如此)。您也对 SQL 注入持开放态度。同样如上所述,当您的 PHP 变量进入数据库时,它们必须用引号括起来,数据库知道它们是字符串。 -
你没有在表单标签中添加method="post"
标签: php mysql forms mysqli http-status-code-404