简体   繁体   中英

How to connect a MySQL db to an html page using php on XAMPP server?

Hi i was trying to connect MySQL database to a simple html code given below.

<!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>

This html code passes the form input "key" to another php file whose code is given below.

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

    ?>

Now when I try to pass a value in the form input in the my browser I get a php code as a result instead of the information in the database which was supposed to to be returned while passing the value in form input, which is also given below(the problem). I am trying to make a project which use this feature as a primary tool so please help as soon as possible.

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

I commented some wrong line in the code and i changed them, just follow it.

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

  ?>

The only issue I see right off the top of my head is you wrote $POST['key']; when it should be $_POST['key']; don't forget the underscore. Visit the PHP manual to learn more , it also helps with debugging code.

On another note, for those hopefully learning from this question, it is good to point out that the code you are using to connect to the database is taking advantage of a work around that doesn't technically use the official Object Oriented method. This allows your PHP code to work in PHP versions prior to 5.2.9/5.3.0 when it was fixed. See the PHP manual for some great explanations on that .

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM