2014-08-30 50 views
0

您好,我试图将MySQL数据库连接到下面给出的简单的HTML代码。如何使用XAMPP服务器上的php连接MySQL数据库到html页面?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org 
/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Untitled Document</title> 
</head> 

<body> 
    <form action="result.php" method="POST"> 
    S.No : 
    <input type="text" name="key"> 
    <input type="submit" value="Search"> 
    </form> 
</body> 
</html> 

该html代码将表单输入“key”传递给另一个php代码如下文件。

<?php 

    $serial=$POST['key']; 

    if(!$serial){ 
     echo 'Please go back and enter the correct value'; 
     exit; 
    } 

    $db = new mysqli('localhost', 'root', '', 'demo_db', 'tbldem'); 
    if(mysqli_connect_errno()) { 
     echo 'Connection lost.. please try again later !!'; 
     exit; 
    } 

    $query = "select * from tbldem where".$serial."like'%".$serial."%'" ; 
    $result = $db->query($query); 

    $num = $result->num_rows; 

    for($i = 0; $i < $num; $i++) { 
     $row = $result->fetch_assoc(); 
     echo"<p>Serial : </p>"; 
     echo $row['Index']; 
     echo"<p>Name : </p>"; 
     echo $row['Name']; 
     echo "<p>Course : </p>"; 
     echo $row['Course']; 
    } 

    $result->free(); 
    $db->close(); 

    ?> 

现在,当我试图在我的浏览器,我收到了PHP代码的结果,而不是它应该同时传递值中要返回在数据库中的信息的形式输入一个值传递表单输入,这也在下面给出(问题)。我正在尝试使用此功能作为主要工具的项目,因此请尽快提供帮助。

query($query); $num = $result->num_rows; 

for($i = 0; $i < $num; $i++) { 
$row = result->fetch_assoc(); echo" 

Serial : 
"; echo $row['Index']; echo" 

Name : 
"; echo $row['Name']; echo " 

Course : 
"; echo $row['Course']; } $result->free(); $db->close(); ?> 
+0

听起来像你的服务器不处理PHP - 是PHP安装,你检查XAMPP指令? – 2014-08-30 03:11:12

+0

您的查询存在主要问题 - >'“select * from tbldem where”。$ serial。“like'%”。$ serial。“%'”'。 1.打开SQL注入。 2.您为列名和值使用相同的发布值。 3.'where','$ serial'和'like'之间没有空格,因此它们全部变为1个字 - >'列列'%column%''。 – Sean 2014-08-30 03:38:58

回答

1

我在代码中注释了一些错误的行,我改变了它们,只是按照它。

<?php 

    $serial=$_POST['key']; // change to $_POST['key']; 

    if(!$serial){ 
    echo 'Please go back and enter the correct value'; 
    exit; 
    } 

    $db = mysqli_connect('localhost','user_name','pass','demo_db'); // dont select your table in this line 

    mysqli_select_db($con,"tbldem"); // select your table here 

    if(mysqli_connect_errno()){ 
    echo 'Connection lost.. please try again later !!'; 
    exit; 
    } 


    $query = "select * from tbldem where serial LIKE '%$serial%';" ; // change $serial to serial like this line 
    $result = $db->query($query); 

    $num = $result->num_rows; 

    for($i=0;$i<$num;$i++){ 
    $row = $result->fetch_assoc(); 
    echo"<p>Serial : </p>"; 
    echo $row['Index']; 
    echo"<p>Name : </p>"; 
    echo $row['Name']; 
    echo "<p>Course : </p>"; 
    echo $row['Course']; 
    } 

    $result->free(); 
    $db->close(); 

    ?> 
+0

不错的mysqli连接。我不习惯看OO(面向对象)代码。我会给出这个答案,但不要忘记看我的答案中的链接。 – Blizzardengle 2014-08-30 03:50:17

0

我从头顶看到的唯一问题是你写了$ POST ['key'];当它应该是$ _POST ['key'];不要忘记下划线。它也有助于调试代码。

另一方面,对于那些希望从这个问题中学习的人来说,指出你用来连接数据库的代码正在利用一种技术上的优势,这在技术上并不使用官方的面向对象方法。这可以让你的PHP代码在5.2.9/5.3.0之前的PHP版本中工作。 See the PHP manual for some great explanations on that

希望这会有所帮助。

相关问题