2016-12-19 45 views
0

我想上傳一個圖像並顯示它與數據庫 我設法使它上傳和連接到數據庫,但它不顯示它 有人可以採取看我在告訴我什麼代碼我做錯了:php上傳圖像和顯示數據庫(錯誤)

的db.php中:

<?php 
$host = "localhost"; 
$username = "root"; 
$password = ""; 
$db_name = "db_php_school"; 

    mysql_connect($host,$username,$password) or die("Database not connect"); 
mysql_select_db($db_name) or die ("could not connect databse"); 
error_reporting("E_ERROR_WORKING"); 
echo "Database connecet success"; 


?> 

這是在index.php:

<?php 


include './classes/image_class.php'; 

$obj_image = new Image(); 

if(@$_POST['Submit']) 
{ 
$obj_image->image_name=str_replace("'", "''", $_POST['txt_image_name']); 
$obj_image->image=str_replace("'", "''", $_POST['txt_image']); 

    $obj_image->Insert_into_image(); 

    $data_image=$obj_image->get_all_image_list(); 
$row=mysql_num_rows($data_image); 
} 

?> 

<!DOCTYPE html> 
<html> 
<head> 
<title>NOvaeye </title> 
</head> 
<body> 
<CENTER><H1>Novaeyewear</H1></CENTER> 
<CENTER><H2>Sunglass</H2></CENTER> 

    <CENTER> 
    <form method="post" enctype="multipart/form-data"> 
    <table border="1" width="80%"> 
    <tr> 
    <th width="50%">Image NAme</th> 
    <td width="50%"><input type="text" name="txt_image_name"></input></td> 
    </tr> 
    <tr> 
    <th width="50%">Upload IMage</th> 
    <td width="50%"><input type="file" name="txt_image"></input></td> 
    </tr> 
    <tr> 
    <td></td> 
    <td width="50%"><input type="submit" name="Submit" value="Submit"></input></td> 
    </tr> 
    </table> 
    </form> 
</CENTER> 

    <?php 
    if($row!=0) 
    { 
    ?> 
    <center> 
    <table width="80%" border="1"> 
    <?php 
    $icount = 1; 
    while($data= mysql_fetch_assoc($data_image)) 
    { 
    ?> 
    <tr> 
     <td style="text-align:center" style="width:10%;"><?php echo $icount; ?></td> 
     <td style="text-align:center" style="width:20%;"><?php echo $data['image_name']?></td> 
     <td style="text-align:center" style="width:50%;><img src="images/<?php echo $data['image']; ?>" width="400px" height="200px"></td> 
    </tr> 
    <?php 
    $icount++; 
    } 
    ?> 
    </table> 
    </center> 
    <?php 
} 

    ?> 
</body> 
</html> 

這就是image_class.php

<?php 
include 'db/db.php' ; 

    class Image{ 

    var 
    $image_id, 
    $image_name, 
    $image; 

    function Insert_into_image(){ 
    if(isset($_FILES['txt_image'])) 
    { 
     $tempname = $_FILES['txt_image']['tmp_name']; 
     $originalname =$_FILES['txt_image']['name']; 
     $size =($_FILES['txt_image']['size']/5242888). "MB<br>"; 
     $type=$_FILES['txt_image']['type']; 
     $image=$_FILES['txt_image']['name']; 
     move_uploaded_file($_FILES['txt_image']['tmp_name'],"images/".$_FILES['txt_image']['name']); 
     } 


    $query = "Insert into t_image_upload 
    (
    image_name, 
    image 
    ) 
    values 
    (
    '$this->image_name', 
    '$image' 
    )"; 
    if(mysql_query($query)){ 
    echo "Insert success"; 
    } 
    else 
    { 
    echo "Insert not success"; 
    } 
    } 

    function get_all_image_list(){ 
    $query = "select *from t_image_upload"; 
    $result = mysql_query($query); 
    return $result; 
    } 

} 
?> 

但問題可能規定了在index.php上線63

<td style="text-align:center" style="width:50%;><img src="images/<?php echo $data['image']; ?>" width="400px" height="200px"></td> 
    </tr> 

這裏有一些屏幕截圖:

enter image description here

enter image description here

+0

image uploaded successfully successfully? –

+0

+0

爲什麼所有u'r id的0 ??你看到什麼時候檢查元素?(F12 in chrome) – Mitch

回答

0

首先,檢查ima ge上傳到文件夾中,然後嘗試給絕對路徑,如

<tr> 
<td style="text-align:center" style="width:50%;> 
<img src="http://localhost/project/images/<?php echo $data['image']; ?>" width="400px" height="200px"> 
</td> 
</tr>