您好,我试图将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(); ?>
听起来像你的服务器不处理PHP - 是PHP安装,你检查XAMPP指令? – 2014-08-30 03:11:12
您的查询存在主要问题 - >'“select * from tbldem where”。$ serial。“like'%”。$ serial。“%'”'。 1.打开SQL注入。 2.您为列名和值使用相同的发布值。 3.'where','$ serial'和'like'之间没有空格,因此它们全部变为1个字 - >'列列'%column%''。 – Sean 2014-08-30 03:38:58