【问题标题】:PHP: Use HTML form to write to database in MySQL Workbench 6.3PHP:在 MySQL Workbench 6.3 中使用 HTML 表单写入数据库
【发布时间】: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


【解决方案1】:

这些文件应该在同一个文件夹中。

您没有使用撇号 (') 将变量绑定到您的查询。

$sql = "INSERT INTO tblcustomers (Company, Position, FirstName, LastName, Address, PhoneNumber, CellNumber, AlternateNumber, EMail) 
                          VALUES ('$Customer', '$Position', '$FirstName', '$LastName', '$Address', '$PhoneNumber', '$CellNumber', '$AlternateNumber', '$EMail')";

您应该使用*_real_escape_string 来防止某些SQL injections

$Company = mysqli_real_escape_string($con, $_POST['Company']);

删除bankyprinting.html 中包含的customers.php,然后按照表单操作标签中的说明将其提交到customers.php 文件。你的表单中有两个动作标签,其中一个应该是方法标签。

<form method = "post" action = "customers.php" id="customers">

对于您的if(isset($_POST["submit"])) 条件,为了让它识别提交的表单,您应该为您的提交按钮添加一个名称标签。

<input type = "submit" name="submit" value = "submit"/>

为什么你的右括号后面有一个分号?删除它。

}; /* AND TURN IT TO */ }

还可以看看prepared statement

【讨论】:

  • 如果 connect.php 在另一个文件中,请尝试将您的需求代码更改为require("/website/directory/connect.php");。只需替换为必要的目录即可。你的 connect.php 目录是什么?
  • 除了数据库本身之外的所有内容都在同一个文件夹中。
  • 数据没有被插入到数据库中(数据库连接的代码是从 MySQL 工作台的实用程序中获取的,如果有区别的话)。表单提交后只会显示一个空白页。
  • 是的,它只会是一个空白页面,因为customers.php 仅用于插入查询。您可以使用header("LOCATION:bankyprinting.html"); 重定向回您的bankyprinting.html 页面。
  • 好的,这很有帮助。但是在尝试将表单的内容插入数据库时​​,我仍然一无所获。我用“test”填充了所有字段值并提交,但在检查数据库本身时没有得到任何结果。
【解决方案2】:

都在错误信息中:

<form action = "post" action = "customers.php" id="customers">

应该是

<form method= "post" action = "customers.php" id="customers">

服务器正在寻找一个名为 post 的文件,但它找不到。

编辑:另外,

如果您更改了 html 文件的内容,并且尚未配置 Apache 服务器的缓存设置,您可能会查看旧的 html 文件而不是更改后的新文件(请参阅mod_expires) .您需要清除浏览器的缓存,或使用CTRL+SHIFT+R 加载页面,以便访问新的 html 文件。当我经常在网页上工作时,这发生在我身上。

【讨论】:

  • 我猜我的帖子在被标记为重复时被编辑了?这是我在上一个问题中提出的问题。从那时起,我编辑了代码以纠正其他几个问题(PHP 注入等)。我发布的是更正的代码。已进行此更改。
  • 代码的当前化身正是我现在发布的。
  • 如果您更改了操作语句,那么您将收到不同的错误消息。如果您认为这不是问题,请在同一目录中创建一个名为 post 的文件,在其中放入一些乱码,然后查看提交表单是否会显示 post 文件。 404 仅用于文件未找到错误,而不是 php 错误或其他任何错误。确保请求的 url 是您期望的文件。
猜你喜欢
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2017-10-20
  • 1970-01-01
  • 2019-05-11
  • 2017-08-08
相关资源
最近更新 更多