如何在XAMPP服务器上使用php将MySQL数据库连接到html页面?

如何在XAMPP服务器上使用php将MySQL数据库连接到html页面?,php,html,mysql,Php,Html,Mysql,您好,我正在尝试将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="C

您好,我正在尝试将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>
<?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();   

    ?>

无标题文件
美国编号:
此html代码将表单输入“key”传递给另一个php文件,其代码如下所示

<!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>
<?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(); ?>
query($query)$num=$result->num\u行;
对于($i=0;$i<$num;$i++){
$row=result->fetch_assoc();echo“
序列号:
“echo$行['Index'];echo”
姓名:
“echo$row['Name'];echo”
课程:
“echo$row['Course'];}$result->free()$db->close();?>

我在代码中注释了一些错误的行,并对它们进行了更改,只需遵循它即可

  <?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();   

  ?>

我脑海中浮现的唯一问题是你写了$POST['key'];什么时候应该是$_POST['key'];别忘了下划线,它还有助于调试代码

另一方面,对于那些希望从这个问题中学习的人来说,最好指出,您用来连接数据库的代码利用了一种技术上不使用官方面向对象方法的变通方法。这允许您的PHP代码在修复后在5.2.9/5.3.0之前的PHP版本中工作


希望这有帮助。

听起来您的服务器没有处理php-是否安装了php,您是否检查了xampp说明?您的查询存在重大问题->
“从tbldem中选择*,其中“$serial.”类似“%”“$serial.”%”
。1.对SQL注入开放。2.您正在为列名和值使用相同的发布值。3.
where
$serial
like
之间没有空格,因此它们都将变成一个单词->
wherecolumnlike“%column%”
。很好地捕捉到了错误的mysqli连接。我不习惯看到OO(面向对象)代码。我会给出答案,但别忘了看看我答案中的链接。