如何在mysql搜索中显示php中的图像

时间:2014-09-30 09:11:49

标签: php mysql

我开始编程PHP。直到我享受,但现在仍然有点困惑。

我可以记录和显示数据库中的图像。但是我试图通过下拉列表进行搜索,但我无法显示任何内容。

你可以帮我说一下我失败的地方吗?

如果在test2.php中我搜索到数据库的最后一条记录,我可以看到图像。但是,如果我试图钻掘我选择的不再有效的ID。

对于给您带来的不便,我深表歉意。

非常感谢你。


     <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">

    </head>
     <?php 
    include('config.php'); 
    $query_parent = mysql_query("SELECT idTask from Tasks") or die("Query failed: ".mysql_error());
    ?>
    <body style="background-color:#A4A4A4;">
    <?php
    if(isset($_POST['search']))
    {
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '*****';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }

    $idTask = $_POST['idTask'];
$_POST['idTask'] = ctype_digit( (string) $_POST['idTask']) ? (int) $_POST['idTask'] : 0;
$_GET['idTask'] = ctype_digit( (string) $_GET['idTask']) ? (int) $_GET['idTask'] : 0;
    echo"<img src='test2.php?image=". $_POST['idTask'] ."'>";
     if (!isset($_FILES["image"]["tmp_name"])) 
                echo "";
            else{
                $image=  addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
                $image_name = addslashes($_FILES['image']['tmp_name']);
                $image_size = getimagesize($_FILES['image']['tmp_name']);

            if($image_size==FALSE)
                echo "That's not an image";
            else{
                mysql_query("select * from tasks where idTask = '". $_POST['idTask'] ."'");

               echo"<img src='test2.php?image=". $_POST['idTask'] ."'>";

            }

            }


    mysql_close($conn);
    }
    else
    {
    ?>

test2.php

<?php

mysql_connect("localhost", "root", "*****") or die (mysql_error());
mysql_select_db('Database') or die (mysql_error());

$result= mysql_query("select * from tasks where idTask = ". $_GET['idTask']);

if( mysql_num_rows($result) ) {
    //No image found...

} else {
    $row = mysql_fetch_assoc($result);
    $image = $row['image'];
    header("Content-type:image/jpeg");
    echo $image;
}
?>

当我进行搜索时,页面显示为空白。

enter image description here

2 个答案:

答案 0 :(得分:1)

你有一些错误,主要是数组索引名称。

  • 您的mysql查询正在使用ididTask,它永远不会有值。 (在两个文件中)

mysql_query("select * from tasks where idTask = '". $_POST['idTask'] ."'");
  • $idTask从包含空格的键中获取其值,但您的表单不会提交任何带空格的名称。

$idTask = $_POST['idTask'];
  • 我会将您的图片调用重做为以下内容,并分别编辑test2.php(使用GET而不是POST

echo"<img src='test2.php?image=". $_POST['idTask'] ."'>";
  • while中无test2.php。如果您采用上一点,请将整个文件更改为

<?php

mysql_connect("localhost", "root", "*****") or die(mysql_error());
mysql_select_db('Database') or die(mysql_error());

$result = mysql_query("select * from tasks where idTask = ". $_GET['idTask']);

if( mysql_num_rows($result) ) {
    //No image found...

} else {
    $row = mysql_fetch_assoc($result);
    $image = $row['image'];
    header("Content-type:image/jpeg");
    echo $image;
}
?>
  • 我会通过简单的检查确保您的$_GET / $_POST

$_POST['idTask'] = ctype_digit( (string) $_POST['idTask']) ? (int) $_POST['idTask'] : 0;
$_GET['idTask'] = ctype_digit( (string) $_GET['idTask']) ? (int) $_GET['idTask'] : 0;
  • 在使用base64_encode()插入数据库之前,我会对图像进行编码。然后,在test2.php使用;

$row = mysql_fetch_assoc($result);
$image = base64_decode($row['image']);
header("Content-type:image/jpeg");
echo $image;

答案 1 :(得分:0)

使用ajax尝试此代码可能会对您有所帮助。 这是从数据库到下拉列表检索英雄名称。

<?php
mysql_connect("localhost","root","");
mysql_select_db("hero");
?>
<!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>
<script>
function showdetails(str)
{
var xmlhttp;
if (str==0)
  { 
 alert("please select an hero");
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("showresult").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","details.php?id="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>
Hero Name:<select name="id" id="id" onchange="showdetails(this.value)">
                <option value="0">Select Hero Name</option>
                <?php

                $sql="select id,name from herophoto";
                $qry=mysql_query($sql);
                $num=mysql_num_rows($qry);
                if($num>0)
                {
                    while($res=mysql_fetch_array($qry))
                {
                ?>
                <option value="<?php echo $res['id'];?>"><?php echo $res["name"];?></option>
                 <?php
                }
                }
                 ?>
             </select>
             <div id="showresult" align="justify"></div>
</body>
</html>

以下是details.php,它会向您显示从下拉列表中选择的图像。

<?php
mysql_connect("localhost","root","");
mysql_select_db("hero");
$id = $_REQUEST["id"];
$sql="select * from herophoto where id='$id'";
$qry=mysql_query($sql);
$num=mysql_num_rows($qry);
if($num>0)
{
    while($res=mysql_fetch_array($qry))
    {
?>
<img src="photo/<?php echo $res['photo'];?>" height="300" width="300"/>
<?php
    }
}
?>