如何使用输入类型框从数据库中选择数据并在同一页面上打印

时间:2014-01-14 07:29:00

标签: php mysql database

这是我的代码,我在这里遇到问题这是一个输入类型标记,而不是使用PHP从数据库中选择数据。 这里是第一部分代码是html格式化广告第二部分是php 我希望从数据库中选择数据,将数据选择到我们在推送车网站上看到的同一页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpSelect</title>
</head>
<body>
Insert Age for search
<form action="#" method="post" >
<input type="text" id="val" name="resValue" />
<input type="submit" value="submit"  /></form>
<?php
if(isset($_POST['submit']))
{
    $res=$_POST['resValue'];

    echo $res;
    }
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT * FROM Persons where Age=25");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

5 个答案:

答案 0 :(得分:0)

尝试将SQL更改为此

SELECT * FROM Persons where Age='".mysql_escape_string($formValue['val'])."'

答案 1 :(得分:0)

将您的选择查询更改为此选项。

SELECT * FROM Persons where Age='".mysql_escape_string($_POST['val'])."'

答案 2 :(得分:0)

我在您的代码中找到的第一个问题是:

<input type="submit" value="submit"  />

它应该是:

<input type="submit" value="submit" name="submit" />

能够得到结果。以下是代码:

<?php
$query = "";
if(isset($_POST['submit']))
{
  $res=$_POST['resValue'];
  $query = " where Age='$res'"
}
//echo $res;
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons $query");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysqli_close($con);
?>

答案 3 :(得分:0)

在您的问题中,表单的所有值都以表单的所有字段的名称获取。\     所以这里应该是

 <input type="submit" name="submit" value="submit"/>

Because in $_POST['submit'] submit is same as button's name.

答案 4 :(得分:0)

$result = mysqli_query($con,"SELECT * FROM Persons where Age="25");