2015-10-15 134 views
0

因此,我正在尝试创建一个HTML表单,它将数据发布到MySQL中使用PHP的客户数据库中,但对于PHP来说却很新颖。PHP:使用HTML表单在MySQL Workbench中写入数据库6.3

目前,只要我尝试使用提交按钮“在此服务器上未找到请求的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(); 
?> 

和 customers.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错误?),我不知道如何通过获取表单以正确注入数据库来解决这个问题。

+0

他们都在同一个文件夹?这四个文件? –

+1

用引号括起变量'''''''Customer'' – Shehary

+0

你不能在html中包含php文件,就像你写的** bankyprinting.html ** – Joomler

回答

0

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

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

$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.htmlcustomers.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

+0

如果connect.php在另一个文件中,请尝试将您的require代码改为'require(“/ website/directory/connect.php”);'。只需更换必要的目录即可。什么是你的connect.php的目录? –

+0

绝对除了数据库本身以外的所有内容都位于同一个文件夹中。 – user3734311

+0

那么你现在的错误是什么? –

0

这一切都在错误消息:

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

应该

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

服务器寻找一个所谓的后文件,该文件无法找到。

编辑:另外,

如果你已经改变了HTML文件的内容,并且您还没有配置Apache服务器的缓存设置,你可能会寻找在旧的HTML文件,而不是新的随着变化(见mod_expires)。您需要清除浏览器的缓存,或者使用CTRL+SHIFT+R加载页面以获取要访问的新html文件。当我在网页上工作的时候,这发生在我身上。

+0

我猜测我的帖子在被标记为重复项时被编辑了?这是我在上一个问题中提出的问题。自那时起我编辑了代码,以纠正其他几个问题(PHP注入等)的问题。我发布的是更正后的代码。这一改变已经完成。 – user3734311

+0

代码的当前版本正是我现在发布的版本。 – user3734311

+0

如果您更改了操作说明,则会出现不同的错误消息。如果您认为这不是问题,请在同一个目录中创建一个名为post的文件,在其中添加一些乱码,然后查看提交表单是否显示您的帖子文件。 404只适用于文件未找到错误,而不是php错误或其他任何内容。确保请求的url是你期望的文件。 – Riet

相关问题