2017-03-05 92 views
0

你好,我有兩臺服務器,一臺帶有表單和mysql數據庫,我把數據插入數據庫。另一臺服務器只有一種表單,我希望將填入表單的數據插入到第一個數據庫服務器中。 如何將seconde服務器的表單安全地插入到數據庫中?兩臺服務器兩個表單和一個數據庫

這是第一個服務器的代碼:

<!doctype html> 
 

 
<html lang="en"> 
 
<head> 
 
    <meta charset="utf-8"> 
 

 
    <title></title> 
 
    <meta name="description" content=""> 
 
    <meta name="author" content=""> 
 

 
    <link rel="" href=""> 
 

 
    
 
</head> 
 

 
<body> 
 
    <script src=""></script> 
 
    
 
    <form action="reg.php" method="post"> 
 
    <label>First name: <input type="text" name="firstname"><br> 
 
    <label>Last name: <input type="text" name="lastname"><br> 
 
    <input type="submit" value="Submit"> 
 
    <input type="reset" value="Reset"> 
 
    </form> 
 
    
 
</body> 
 
</html>

<?php 
 
\t if(empty($_POST["firstname"]) OR empty($_POST["lastname"])) { 
 
\t \t header("refresh:5; url=registration.html"); 
 
\t \t echo "please fill all the fields."; 
 
\t } 
 
\t else { 
 
\t \t $con=mysqli_connect("localhost","root","","form"); 
 
\t \t if(!$con) { 
 
\t \t \t die(" error: ". mysql_error()); 
 
\t \t } 
 
\t \t $firstname=$_POST['firstname']; 
 
\t \t $lastname=$_POST['llastname']; 
 
\t \t $sql= "INSERT INTO a_database (id, firstname, lastname,) 
 
\t \t VALUES('DEFAULT','$firstname','$lastname',CURRENT_TIMESTAMP)"; 
 
\t \t if(mysqli_query($con,$sql)) { 
 
\t \t \t echo "success"; 
 
\t \t } 
 
\t \t else { 
 
\t \t \t echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
 
\t \t } 
 
\t \t 
 
\t } 
 
\t mysqli_close($con); 
 
?>

回答

0

在你的第二個服務器的PHP代碼,你只需要更改數據庫地址:

$con=mysqli_connect("<first server address>","root","","form"); 

然後,在第一臺服務器上檢查防火牆規則(MySQL默認使用端口3306)和MySQL配置,以允許來自第二臺服務器的遠程SQL連接。

+0

謝謝你我使用你的soloution – George

0

這很簡單,您只需將「action」屬性的值設置爲運行服務器端腳本的路徑即可。

你必須公正,確保<形式行動= 「http://yourdomain.com/reg.php」 方法= 「郵報」 >是相同的兩種形式

<!doctype html> 
 

 
<html lang="en"> 
 
<head> 
 
    <meta charset="utf-8"> 
 

 
    <title></title> 
 
    <meta name="description" content=""> 
 
    <meta name="author" content=""> 
 

 
    <link rel="" href=""> 
 

 
    
 
</head> 
 

 
<body> 
 
    <script src=""></script> 
 
    
 
    <form action="http://yourdomain.com/reg.php" method="post"> 
 
    <label>First name: <input type="text" name="firstname"><br> 
 
    <label>Last name: <input type="text" name="lastname"><br> 
 
    <input type="submit" value="Submit"> 
 
    <input type="reset" value="Reset"> 
 
    </form> 
 
    
 
</body> 
 
</html>

+0

感謝您的巡演答案 – George

相關問題