2015-10-15 209 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

相關問題