2015-11-02 57 views
-4

好吧,我能寫出整個代碼,所以我現在可以將數據添加到我的數據庫。但我仍然唯一的問題是,我希望我輸入到html字段中的文本顯示在數據庫中。那麼,我如何讓我輸入到html文件中的數據進入php文件,然後將數據發送到數據庫?如何將html數據發送到我的php文件?

代碼:

<html> 
    <head> 
     <title>Tabelle</title> 
     <form action="phpRICHTIG.php" action="post"/> 
     Test1: <input type="text" name="Test1"/><br/> 
     Test2: <input type="text" name="Test2"/><br/> 
     Test3: <input type="text" name="Test3"/><br/> 
     Test4:<input type="text" name="Test4"/><br/> 
     <input type="submit" value="Absenden"/> 
     </form> 
     </body> 
</html> 

<?php 

include "htmltest.php"; 

$link = mysqli_connect("localhost", "root", "", "vanille"); 


if($link === false){ 
die("Konnte nicht verbinden. " . mysqli_connect_error()); 
} 


$sql = "INSERT INTO vanille (Test1, Test2, Test3, Test4) VALUES ('test', 'test', 'test', 'test')"; 
if(mysqli_query($link, $sql)){ 
echo "Hat geklappt."; 
} else{ 
echo "Hat nicht geklappt und das hat nicht funktioniert: $sql. " .   mysqli_error($link); 
} 


mysqli_close($link); 
?> 
+0

的HTML代碼htmltest.php和PHP代碼是phpRICHTIG BTW – DarkYagami

+0

@GourabNag是否有幫助 – DarkYagami

+0

首先做你的數據庫確實有4列,爲test1,test2的,test3和test4? – Nirnae

回答

1

檢查變量設置,如果是這樣,則定義它們。然後使用查詢插入這些值。請參見下面的代碼:

<?php 

//include "htmltest.php"; you don't need to do this 

$link = mysqli_connect("localhost", "root", "", "vanille"); 


if($link === false){ 
die("Konnte nicht verbinden. " . mysqli_connect_error()); 
} 

if (isset($_POST['Test1']))//check if it's set 
    $test1 = $_POST['Test1'];//define it 

if (isset($_POST['Test2'])) 
    $test2 = $_POST['Test2']; 

if (isset($_POST['Test3'])) 
    $test3 = $_POST['Test3']; 

if (isset($_POST['Test4'])) 
    $test4 = $_POST['Test4']; 
//make sure you escape values before inserting it to database 
$sql = "INSERT INTO vanille (Test1, Test2, Test3, Test4) VALUES ('".$test1."', '".$test2."', '".$test3."', '".$test4."')"; 

//then insert it into your database through the query 

if(mysqli_query($link, $sql)){ 
echo "Hat geklappt."; 
} else{ 
echo "Hat nicht geklappt und das hat nicht funktioniert: $sql. " .   mysqli_error($link); 
} 


mysqli_close($link); 
?> 

你最好有表單提交前的數據驗證,雖然,以確保該字段不爲空。否則,你會得到一個錯誤,或者你會將空值插入到數據庫中。

+0

夥伴我不能在這裏爲他寫一個完整的Web應用程序,我沒有像你那樣使用諷刺嗎? – Sina

+0

對不起,我有點太無禮,我已經恢復了我的投票,以upvote –

+0

不用擔心隊友,我明白你喜歡他們的代碼都保證;),謝謝! – Sina

2

這裏是你的代碼更新:

<?php 
//if the code on phpRICHTIG.php and htmltest.php is the same then you did need this line 
//include "htmltest.php"; 

$link = mysqli_connect("localhost", "root", "", "vanille"); 


if($link === false){ 
die("Konnte nicht verbinden. " . mysqli_connect_error()); 
} 


//First we check if we have some data posted . 
if(isset($_POST)){ 

    //Then we set them to var 
    //You will have to securize that by yourself later, insert directly $_POST is a huge security breach 
    $test1 = $_POST['Test1']; 
    $test2 = $_POST['Test2']; 
    $test3 = $_POST['Test3']; 
    $test4 = $_POST['Test4']; 

    $sql = "INSERT INTO vanille (Test1, Test2, Test3, Test4) VALUES ('$test1', '$test2', '$test3', '$test4')"; 


    if(mysqli_query($link, $sql)){ 
    //It worked 
    echo "Hat geklappt."; 
    } else { 
    //It failed 
    echo "Hat nicht geklappt und das hat nicht funktioniert: $sql. " .   mysqli_error($link); 
    } 

}else{ 

    echo 'No form submitted'; 
} 


mysqli_close($link); 
?> 
+1

這裏的重點不是爲他做所有的工作,而只是爲了給他指導思路,這看起來像一些測試的僞代碼,所以我不認爲他冒着這麼多風險,但即使如此,我仍然阻止他那個服務器需要驗證是爲了避免SQL注入 – Nirnae

+0

沒問題,你的回答比值得,但這個問題真的很糟糕 –

相關問題