简体   繁体   English

HTML表单未提交到MYSQL数据库

[英]HTML Form Not Submitting to MYSQL Database

I have been trying to get this to work for some hours. 我一直试图让它工作几个小时。 I have researched many threads on here trying to see if I could find the problem with my code. 我在这里研究了很多线程,试图看看我是否能找到我的代码问题。 I am new to PHP and I keep getting a Internal Server error, but I cant seem to track it down. 我是PHP的新手,我不断收到内部服务器错误,但我似乎无法追踪它。 I have tried all sorts of methods suggested online to get this to work with no luck. 我已经尝试过网上建议的各种方法,但没有运气。 Its a basic user signup form in HTML, in a PHP file.(I was going to do both html and php on the same file but could not get that to work) The idea is to have the form submit to my MYSQL database to a customer table. 它是HTML中的一个基本用户注册表单,在一个PHP文件中。(我打算在同一个文件上同时执行html和php但无法使其工作)这个想法是让表单提交给我的MYSQL数据库客户表。 If any of you could shed some light on what I am doing wrong or point me in the right direction, it would be much appreciated. 如果你们中的任何一个人能够阐明我做错了什么或者指出了我正确的方向,那将非常感激。 Thanks in advance. 提前致谢。

HTML HTML

<form id="signupField" action="register.php" method="post">
First Name:<br>
<input type="text" name="FN" size="auto"/><br>
Last Name:<br>
<input type="text" name="LN" size="auto"/><br>
Street Address:<br>
<input type="text" name="SA" size="auto"/><br>
City:<br>
<input type="text" name="City" size="auto"/><br>
State:<br>
<input type="text" name="ST" size="auto"/><br>
Zip:<br>
<input type="text" name="Zip" size="auto"/><br>
Email Address:<br>
<input type="text" name="Email" size="auto"/><br>
Password:<br>
<input type="text" name="Password" size="auto"/><br>
<input type="submit" value="submit" name="submit"/>
</form>

Referenced PHP: 引用的PHP:

<?php 
$hostname = "localhost";
$username = "serviceaccount";
$password = "password";
$dbname = "nameofdb";
$conn = new mysqli($hostname,$username,$password,$dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

?>
<?php

$FName=$_POST["FN"];
$LName=$_POST["LN"];
$SA=$_POST["SA"];
$City=$_POST["City"];
$State=$_POST["ST"];
$Zip=$_POST["Zip"];
$Email=$_POST["Email"];
$Password=$_POST["Password"];

if (isset($_POST["submit"])) {

$sql = "INSERT INTO Customers(FName,LName,StreetAddress,City,State,Zip,Email,Password) VALUES('$_POST["FN"]','$_POST["LN"]','$_POST["SA"]','$_POST["City"]','$_POST["ST"]','$_POST["Zip"]','$_POST["Email"]','$_POST["Password"]')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}

$conn->close();
}
}
?>

EDIT After finding this error: Connection failed: Unknown MySQL server host 'localhost:3306' (0) I was able to solve the connection issue to the database. 编辑找到此错误后:连接失败:未知MySQL服务器主机'localhost:3306'(0)我能够解决数据库的连接问题。 Now when I put in the rest of the PHP back in I get a 500 Error still. 现在,当我把剩下的PHP放回去时,我仍然得到500错误。 It definitely narrows down where the issue is though! 它肯定会缩小问题所在!

EDIT I want to thank you all for you help on here. 编辑我想感谢大家的帮助。 The main issue was the SQL connection. 主要问题是SQL连接。 After I got that taken care of, I found that I had an extra bracket in place which was the cause for the other internal error I was receiving. 在我得到照顾之后,我发现我有一个额外的支架,这是我收到的另一个内部错误的原因。

Replace your register.php code with following. 用以下内容替换register.php代码。

<?php 
$hostname = "localhost";
$username = "serviceaccount";
$password = "password";
$dbname = "nameofdb";
$conn = new mysqli($hostname,$username,$password,$dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

if it prints that connection failed; 如果打印出该连接失败; then you have to check your code for connecting to database. 那么你必须检查你的代码连接到数据库。

Use isset($_POST["FN"]) .. etc for all post variables and try to use variables $FName , $LName etc. in your insert query instead of direct $_POST variables. 对所有post变量使用isset($ _ POST [“FN”]).. etc并尝试在insert查询中使用变量$ FName,$ LName等而不是直接$ _POST变量。 eg 例如

$FName = '';
if(isset($_POST["FN"]) && $_POST["FN"] !=''){
    $FName = $_POST["FN"];
}

Also check whether connection is opening or failing. 还要检查连接是打开还是失败。

before insert any new field into your db use 在将任何新字段插入数据库使用之前

mysql_real_escape_string mysql_real_escape_string

check for empty params before insert these to db 在将这些插入到db之前检查空参数

use the following to turn all error reporting on for MySQLi 使用以下内容为MySQLi打开所有错误报告

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

First, the "basic check" question: the html with the form and register.php are in the same directory, right? 首先,“基本检查”问题:带有表单和register.php的html都在同一个目录中,对吗? Not includes, requires, etc 不包括,要求等

If you have access to the apache error_log, check it out first to see the error message you're getting. 如果您有权访问apache error_log,请先检查它以查看您收到的错误消息。 If you can't understand it, post it here to help you. 如果您无法理解,请在此处发布以帮助您。

If you don't have acccess to the file, first we need to find where are you getting the mistake. 如果你没有对文件进行处理,首先我们需要找到你弄错的地方。 As an idea, start with this in register.php... 作为一个想法,从register.php开始...

<?php 
$hostname = "localhost";
$username = "serviceaccount";
$password = "password";
$dbname = "nameofdb";
$conn = new mysqli($hostname,$username,$password,$dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

?>

Got to http://[yourdirectory]/register.php DIRECTLY and see what you get. 得到http:// [yourdirectory] ​​/register.php直接看看你得到了什么。 Then start adding the rest step by step and let us know when you get the mistake. 然后开始逐步添加其余部分,并在出现错误时通知我们。

Other think to check is the database NULL variables configuration. 其他想想检查的是数据库的NULL变量配置。 All the columns allow null values? 所有列都允许空值? Are you filling all form fields or are you leaving any field empty? 您是填写所有表单字段还是将任何字段留空?

You should use your variables that you assigned for your post fields instead of the $_POST-objects and make sure you sanitize all user inputs. 您应该使用为post字段分配的变量而不是$ _POST对象,并确保清理所有用户输入。

You could try replacing your $sql-string with the following: 您可以尝试使用以下代码替换$ sql-string:

$sql = "INSERT INTO Customers(FName,LName,StreetAddress,City,State,Zip,Email,Password) VALUES('$FName','$LName','$SA','$City','$State','$Zip','$Email','$Password')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM