无法在数据库中将已保存的图像显示为blob

时间:2016-01-19 22:31:10

标签: php mysql database blob

提前感谢您的帮助。

我有一个名为Noticia的表,它有5个字段(id(int,autoincrement),titulo(varchar 200),noticia(Text 1000),foto(blob)和aphototype(varchar50))

在这种情况下,我可以使用代码

将数据保存到表中
<?php require_once('connectiononly.php'); ?>
<?php

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){

$titulo = $_POST['titulo'];
$noticia = $_POST['noticia'];
$foto = addslashes (file_get_contents($_FILES['foto']['tmp_name']));
$image = getimagesize($_FILES['foto']['tmp_name']);//to know about image type etc

$imgtype = $image['mime'];

$q ="INSERT INTO noticia VALUES('','$titulo','$noticia','$foto','$imgtype')";

mysql_select_db($database_TRS, $TRS);
  $r = mysql_query($q, $TRS) or die(mysql_error());
}

mysql_select_db($database_TRS, $TRS);
$id = $_GET['id'];
$querytype = "SELECT foto,aphototype FROM noticia where id='$id'";
$type = mysql_query($querytype, $TRS) or die(mysql_error());

mysql_select_db($database_TRS, $TRS);
$query_show = "SELECT * FROM noticia";
$show = mysql_query($query_show, $TRS) or die(mysql_error());
$row_show = mysql_fetch_assoc($show);
$totalRows_show = mysql_num_rows($show);

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> Creacion de Noticia</title>
    <meta charset="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="Estilos.css"/>
    <style>
    table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
    }
    th, td {
        padding: 5px;
}
</style>
</head>
<body>
<div class="contenedor">
    <body>
<header>
            <div id="subheader">
                <div id="logotipo"> <p><img class="thumb1" src="alianzafc.jpg" alt="thumbail #2" />
</p>

</div>
</div>
</div>
        </header>
<section id="contenido">
                            <article>
                                <hgroup><h2 class="Titulo1" align="center">Crear nueva Noticia</h2></hgroup>
</br>

<form enctype="multipart/form-data" action="<?php echo $editFormAction; ?>" method="POST">

<table border=0 align=center>
<td>Titulo de la Noticia</td><td><input type=text name="titulo"></td>
</tr>
<tr>
<td>Contenido de la Noticia</td><td><input type=text name="noticia"></td>
</tr>
<tr>
<td>Foto de la Noticia</td><td><input type=file name="foto"></td>
</tr>
<tr>
<td></td><td><input type=submit name="submit" value="Guardar Noticia"></td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<div align="center">
  <table width="561" border="5" bgcolor="#0033FF" >
    <tr>
      <th width="189" scope="col"><font color="white">Titulo:</font></th>
      <th width="192" scope="col"><font color="white">Contenido:</font></th>
      <th width="192" scope="col"><font color="white">Foto:</font></th>
     <th width="73" scope="col"><font color="white">Borrar</font></th>
      </tr>
      <?php  while ($row_show = mysql_fetch_assoc($show)); ?>
      <tr bgcolor="#FFFFFF">
      <th scope="col" ><?php echo $row_show['titulo']; ?></th>
      <th scope="col" ><?php echo $row_show['noticia']; ?></th>
      <th scope="col" ><?php $type = "Content-type: ".$row['aphototype']; header($type); echo "<img src= $querytype?id=".$row_show['id']." width=300 height=100/>";?></th>
       <th scope="col" ><a href="#"><img src="eliminar.png" alt="Delete" width="16" height="16" /></a></th>
          </tr>

    <tr>
      <th colspan="4" scope="col" >

      </th>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</div>
<p>&nbsp;</p>
</body>
</html>

<?php
mysql_free_result($show);

mysql_free_result($type);

?>

问题是显示无效的数据

<p>&nbsp;</p>
    <div align="center">
      <table width="561" border="5" bgcolor="#0033FF" >
        <tr>
          <th width="189" scope="col"><font color="white">Titulo:</font></th>
          <th width="192" scope="col"><font color="white">Contenido:</font></th>
          <th width="192" scope="col"><font color="white">Foto:</font></th>
         <th width="73" scope="col"><font color="white">Borrar</font></th>
          </tr>
          <?php  while ($row_show = mysql_fetch_assoc($show)); ?>
          <tr bgcolor="#FFFFFF">
          <th scope="col" ><?php echo $row_show['titulo']; ?></th>
          <th scope="col" ><?php echo $row_show['noticia']; ?></th>
          <th scope="col" ><?php $type = "Content-type: ".$row['aphototype']; header($type); echo "<img src= $querytype?id=".$row_show['id']." width=300 height=100/>";?></th>
           <th scope="col" ><a href="#"><img src="eliminar.png" alt="Delete" width="16" height="16" /></a></th>
              </tr>

        <tr>
          <th colspan="4" scope="col" >

          </th>
        </tr>
      </table>

任何想法我正在做什么错误以及如何解决它?

由于

0 个答案:

没有答案