2015-11-02 54 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

没问题,你的回答比值得,但这个问题真的很糟糕 –

相关问题