使用 $.post() jquery 和 php 显示从数据库返回的图像时出现问题


Having issue showing image returning from database with $.post() jquery and php

作为搜索操作的结果,我正在从数据库中检索一个表,但是当我尝试显示结果时,我看到了表和数据,但是每行返回的图像没有呈现,我认为我的问题出在$('#search'(.html(data(,我不确定请有人知道问题是什么?

这是结果

http://s9.postimg.org/mro5qn46n/search_result.jpg

这是显示结果表的搜索页面****

<table align="center">
  <tr>
     <td>
     <label for="criteria">Select Criteria</label>
     </td>
    <td>
       <select name="select" id="criteria">
           <option selected="true" style="display:none;"></option>              
           <option value="value1">name</option> 
           <option value="value2">apartment</option>
      </select>
  </td>
  <td>
      <input type="text" name="value" size="40" maxlength="60" id="value"'>
  </td>
  </tr>
  <tr>
  <td>
      <input name="name-submit" type="button" id="submit" value="Search"'>
  </td>
  </tr> 
  <tr>
  <td >
  <div id="search"></div>
  </td>
  </tr> 
</table> 
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
    var criteria = $("#criteria option:selected").text();
    var    value = $("#value").val();
    $.post("search_r.php",{criteria:criteria,value:value},function(data){
        $('#search').html(data);
          });
    });
</script> 

这是在主搜索页面中调用 $.post(( 的页面 ****

<?php
$criteria = $_POST['criteria'];
  $value = $_POST['value'];
      $con = mysqli_connect("localhost","root","");
            mysqli_select_db($con,"gables");
   $query = "SELECT * FROM residents WHERE $criteria = '$value'";       
  $result = mysqli_query($con,$query);
    echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['0'] . "</td>";
  echo "<td>" . $row['1'] . "</td>";
  echo "<td>" . $row['2'] . "</td>";
  echo "<td>" . $row['3'] . "</td>";
  echo "<td>" . $row['4'] . "</td>";
  echo "<td>" . $row['5'] . "</td>";
  echo "<td>" . $row['6'] . "</td>";
  echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";   
  echo "</tr>";
}
echo "</table>";

?>

这里get_image.php****

<?php
$con = mysqli_connect("localhost","root","");
       mysqli_select_db($con,"gables");
$id = $_GET['id'];
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');
echo $picture['11'];
?>

您可以按此方式更改get_image.php文件。那么这项工作就会起作用。

<?php
function get_image($id){
$con = mysqli_connect("localhost","root","");
       mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
return $picture['11'];
}
?>

然后像这样使用require_once();函数和图像 src。echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>"; .在您的代码中,检查 src 路径将如何打印,它将像回显字符串一样打印,而不是作为执行文件。

echo 
    "<table border='1'>
    <tr>
    <th>Id</th>
    <th>Name</th>
    <th>Last Name</th>
    <th>Apartment</th>
    <th>Parking</th>
    <th>Phone1</th>
    <th>Phone2</th>
    <th>image</th>
    </tr>";
    require_once('search_r.php');
    while($row = mysqli_fetch_array($result))
    {
      echo "<tr>";
      echo "<td>" . $row['0'] . "</td>";
      echo "<td>" . $row['1'] . "</td>";
      echo "<td>" . $row['2'] . "</td>";
      echo "<td>" . $row['3'] . "</td>";
      echo "<td>" . $row['4'] . "</td>";
      echo "<td>" . $row['5'] . "</td>";
      echo "<td>" . $row['6'] . "</td>";
      echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";   
      echo "</tr>";
    }
    echo "</table>";