MySQL查询无法读取PHP变量


MySQL query unable to read PHP variables

我有一个HTML表单(Datadisplay.HTML),它使用POST方法提供(Datadisplay.php)变量名称和模型,这些变量集成到SQL查询($SQL)中。问题是php没有显示任何错误,但我的查询在HTML中显示为空白。

事实上,我知道其余部分是有效的,因为当我为字符串替换$this->name$this->model时,它确实会调出信息。任何帮助都将不胜感激。

MYSQL TABLE AS
 id int(11)
 nombre varchar(100)
fecha_remision date
modelo varchar(100)
serial varchar(100)
numero_arraigo varchar(100)

数据显示PHP-

<?php
//ERROR_REPORTING( E_ALL | E_STRICT );
 if(isset($_POST['type'])){
class DataDisplay
{
//Variable for MySql connection
  private $hookup;
 private $sql;
    private $tableMaster;
private $name;
      private $model;

 public function __construct()
 {
    //Get table name and make connection
    $this->table=$_POST['type'];
    $this->hookup=UniversalConnect::doConnect();
    $this->doDisplay();
    $this->hookup->close(); 
    $this->name=$_POST['name'];
    $this->model=$_POST['model']; 
}
 private function doDisplay()
{
        //Create Query Statement
    $this->sql ="SELECT * FROM $this->table WHERE modelo='$this->model' AND    
  nombre='$this->name'";
    try
    {
    $result = $this->hookup->query($this->sql);
    while ($row = $result->fetch_assoc()) 
    {
         echo "<tr>";
 echo "<td>id " . $row['id'] . "</td></br>";
 echo "<td>Nombre " . $row['nombre'] . "</td></br>";
 echo "<td>fecha de remision " . $row['fecha_remision'] . "</td></br>";
 echo "<td>serial " . $row['serial'] . "</td></br>";
 echo "<td>modelo " . $row['modelo'] . "</td></br>";
 echo "<td>numero de arraigo " . $row['numero_arraigo'] . "</td></br>";
 echo "</tr></br>";

     }
    $result->close(); 
    }
    catch(Exception $e)
    {
      echo "Here's what went wrong: " . $e->getMessage();
    }
 }
}
}
else
    {
    echo"<script>alert('selecciona tipo de activo');</script>";
    }
?>

数据显示html

<!doctype html>
 <html>
 <head>
 <link rel="stylesheet" type="text/css" href="css/easy.css">
 <meta charset="UTF-8">
 <title>SCAF</title>
 </head>
 <body>
 <header>
 </header>
 <div>
 <h2>Consulta</h2>
 </div>
 <form action="Client.php" method="post" target="feedback">
 <div><strong>informacion del activo:</strong><br />
   <label for="name">Name:</label>
 <input type="text" name="name" id="name">
 <br />
 <!--cambiar modelo por proyecto(propiedad de tabla), tambien cambiar por radios de  
 nombre de proyectos-->
 <label for="model">modelo:</label>
 <input type="text" name="model" id="model">
 <br />
 <p />
 </div>
  <div><strong>categoria</strong><br />
<label for="cs2">transporte:</label>
<input type="radio" name="type" id="cs2" value="transporte">
<br/>
<label for="cs1">maquinaria:</label>
<input type="radio" name="type" id="cs1" value="maquinaria">
<br/>
<label for="cs">maquinaria pesada:</label>
<input type="radio" name="type" id="cs" value="maquinaria pesada">
<br />
 </div>
 <br />
   <div>
   <input type="submit" name="all" value="buscar">
  </div>
 </form>
 <p />
 <div><strong></strong>
  <p />
  </div>
  <iframe name="feedback" seamless width="300" height="200">PHP</iframe>
 <div>
 <p>
 <a href="EasyUpdateOOP.html">Update and Drop Files</a>
 </p>
 </div>
 </body>
 </html>

如果您想在双引号中解析对象属性,则需要将其括在大括号中:

$this->sql = 
   "SELECT * 
    FROM {$this->table} 
    WHERE modelo='{$this->model}' AND nombre='{$this->name}'";

好的,很难判断你的问题是什么,但从你发布的内容来看,有几个问题。以下是要查看的内容:

1) 我看到你已经根据if子句的结果定义了一个类。在10年的编程生涯中,我从未见过有人这样做,他们通常只是定义类,然后在需要时使用它。

2) 我看到你在定义类,但我看不到你在任何地方实际实例化该类类型的对象或调用任何函数。试着在不同的地方放一些简单的:echo('hello')语句,看看你是否已经进入了你的代码

3) 正如另一位评论者所说,你不能把对象引用放在双引号里。你不能这样做:

$whatever = "foo bar $this->derp whatnot";

你可以这样做:

$whatever = "foo bar " . $this->derp ." whatnot";

或者(如果我错了,请纠正我)我想你也可以这样做:

$whatever = "foo bar {$this->derp} whatnot";

检查你的代码是否有这些类型的错误,我想你会发现你的问题

PHP字符串插值在文档中有很好的介绍:

http://php.net/manual/en/language.types.string.php#language.types.string.parsing.simple

尝试字符串插值的"复杂"形式:

// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']} and produces the expected output.";

另一种选择是使用级联:

echo "This works: " . $arr['key'] . " and produces the expected output.";

请注意,将POST中的值插入SQL查询是一个第一级安全漏洞,因为它允许SQL注入攻击。为了互联网的安全,请使用PDO准备的语句系统来替换SQL语句,不要这样做,你是在为黑客提供进入服务器的网关,因为你这样做的方式允许攻击者插入任意的SQL子查询,这些子查询可以做任何事情,直到将数据库删除到SQL查询中!

http://php.net/manual/en/pdo.prepare.php