无法显示数据库中的图像

时间:2016-10-27 11:01:44

标签: php mysql image

我是php新手,特此我正在做一个简单的选美网站,现在我想显示数据库中的所有参赛者图片。我的数据库包含名称,地址,phoneno和图像(VARCHAR)。我上传了几个参赛者照片,但是当我拿到它时只显示一张空白图片,我的代码如下: -

    <?php

    include("Connection.php");
    $con=mysqli_connect("localhost","","",""); // Change it if required

    // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT image FROM contestant WHERE Gender='Male' " );


    while($row = mysqli_fetch_array($result))
    {
    echo '<img src="data:image/png;base64' . $row['image'] . '" width="200" />';
    echo'<br /><br />';  
    }


    mysqli_close($con);

    ?>

任何人都可以帮我弄清楚我的代码出错了吗?非常感谢您的帮助。以下是我如何将参赛者形象从php发送到数据库

include("Connection.php");?>
  <?php
    function GetImageExtension($imagetype)
    {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }

     }
?>
<?php
// create a variable
$Name =$_POST['Name'];
$NRIC =$_POST['NRIC'];
$Gender=$_POST['Gender'];
$Address =$_POST['Address'];
$Mail_ID =$_POST['Mail_ID'];
$Password =$_POST['Password'];
$yearOfBirth = $_POST['yearOfBirth'];
$monthOfBirth = $_POST['monthOfBirth'];
$dateOfBirth = $_POST['dateOfBirth'];

$Phone_No =$_POST['Phone_No'];


 $image=$_FILES['image']['name'];
$imagedata = file_get_contents($_FILES["image"]["tmp_name"]);
 $imgtype=$_FILES["image"]["type"];
 $ext= GetImageExtension($imgtype);
$target_path = "images/".$image;
$img = file_get_contents($image);
$base64 = base64_encode($imagedata);
if(move_uploaded_file($tmp_name, $target_path)) 



$video_path=$_FILES['file']['name'];
$Description = $_POST['Description'];
// Validate
if ($yearOfBirth != '' && $monthOfBirth != '' && $dateOfBirth != '') {
  // Generate date of birth in format of YYYY-mm-dd
  $Date_of_Birth = $yearOfBirth.'-'.$monthOfBirth.'-'.$dateOfBirth;
//validate image 

 mysqli_stmt_bind_param($stmt, "s",$img);
    mysqli_stmt_execute($stmt);

    $check = mysqli_stmt_affected_rows($stmt);
    if($check==1){
        $msg = 'Successfullly UPloaded';
    }else{
        $msg = 'Could not upload';
    }
    //validate
    //$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$imageFileType= $_FILES["fileToUpload"]["type"];

if($imageFileType != "video/mp4" && $imageFileType != "video/avi" && $imageFileType != "video/mov" && $imageFileType != "video/3gp" && $imageFileType != "video/mpeg")

//Execute the query

$sql= ("INSERT INTO contestant (Name,NRIC,Gender,Address, Mail_ID,Password,Date_of_Birth,Phone_No,image,Video,Description) VALUES ('$Name','$NRIC','$Gender','$Address', '$Mail_ID','$Password','$Date_of_Birth','$Phone_No','$image_data','$video_path','$Description')");

        //executen the sql statement
        if (mysql_query($sql,$conn))
{ 

if(isset($_POST['Submit'])) {
  //code for the php form

}else {
  //code to display form

}
echo '<table>';
echo'<center>';




    echo "<strong>Name:</strong> ".$Name."<br />";
echo "<strong>Gender:</strong> ".$Gender."<br />";
echo "<strong>Address:</strong>".$Address."</i><br />";
echo "<strong>Email:</strong> ".$Mail_ID."<br />";
echo "<strong>Date of Birth:</strong> ".$Date_of_Birth."<br />";
echo "<strong>Phone No:</strong> ".$Phone_No."<br />";

echo '</table>';



    }



    else {
        echo'<center>';
        echo "<strong>The Contestant Is Already Exists</strong>";
        //echo the result identifier
    }
}
    echo'</center>';    ?>
            </p>
            <p align="center"><img src="data:image/png;base64, <?php echo $base64; ?>" width="200" height="200" border="2" align="texttop" /></p>

0 个答案:

没有答案