Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用输入类型框从数据库中选择数据并在同一页上打印_Php_Mysql_Database - Fatal编程技术网

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

Php 如何使用输入类型框从数据库中选择数据并在同一页上打印,php,mysql,database,Php,Mysql,Database,这是我的代码,这里我遇到了一个问题,这是一个输入类型标记,不是使用PHP从数据库中选择的数据。 这里是这段代码的第一部分是html格式的广告,第二部分是php 我想从数据库中选择数据,然后将数据扫描到我们在sopping购物车站点上看到的相同页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <html xmlns="http://www.w3.org/1999/xhtml"> &

这是我的代码,这里我遇到了一个问题,这是一个输入类型标记,不是使用PHP从数据库中选择的数据。 这里是这段代码的第一部分是html格式的广告,第二部分是php 我想从数据库中选择数据,然后将数据扫描到我们在sopping购物车站点上看到的相同页面

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

尝试将SQL更改为


选择*来自年龄='.mysql\u escape\u字符串$formValue['val'].

将SELECT查询更改为此查询

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

我在您的代码中发现的第一个问题是:

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

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

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

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

$result=mysqli\u查询$con,从年龄=25的人员中选择*

您必须更改选择以获取文本框。您可以添加一些有关代码工作方式的额外信息吗?