2016-02-13 29 views
0

我正嘗試在MySql中顯示上傳到我的「上載」表中的圖像。我一直在閱讀如何做到這一點,但沒有運氣。如何使用特殊日期而不是通過id顯示存儲在Mysql數據庫中的圖像(php)

「news_content」 表上傳新聞內容到MySQL,並有6列:ID,標題,描述,content_text,日期,時間

和 「上傳」 表有5列:ID,名稱,規格,圖片,日期

在「news_content」表中,我分別上傳日期和時間列,但「上傳」表中的日期列是一個與日期和時間連接的字符串。例如,如果在「news_content」表中日期爲2/3/2016並且時間爲5:30,則在「上傳」表格中日期將是2/3/20165:30。我以這種方式組織它,以便通過相關帖子上傳的特定日期和時間來檢索圖像。

我上傳news.php頁面圖像與此下面的代碼:

news.php:

// Create connection 
$connection = mysql_connect("localhost","root","p206405az"); 

// Check connection 
if (!$connection) { 
    die("Connection failed: " . mysql_error()); 
} 
//select a database to use 
$db_select = mysql_select_db("news" , $connection) ; 
    if (!$db_select) { die("Selection faild:" . mysql_error()) ; 
} 
//uploading the content of news and date and time 
if(isset($_POST["submit"])){ 

    $title = $_POST["title"] ; 
    $description = $_POST["description"] ; 
    $content_text = $_POST["content_text"] ; 
    $date = $_POST["date"] ; 
    $time = $_POST["time"] ; 
//perform mysql query 
    $result = mysql_query("INSERT INTO news_content (title, description, content_text, date, time) 
     VALUES ('$title', '$description', '$content_text' ,'$date' , '$time')") ; 

    if (!$result) { 
     die("Insert failed: " . mysql_error()); 
     $submitMessage = "Problem with updating the post, please try again" ; 

    } else if ($result){ 

     $submitMessage = "Your post succsessfully updated" ; 
    } 

} 
// uploading the image 
if(isset($_POST['submit']) && $_FILES['image']['size'] > 0) 
{ 
    $fileName = $_FILES['image']['name']; 
    $tmpName = $_FILES['image']['tmp_name']; 
    $fileSize = $_FILES['image']['size']; 
    $fileType = $_FILES['image']['type']; 
    $filedate = "$date" . "$time" ; 

    $fp  = fopen($tmpName, 'r'); 
    $content2 = fread($fp, filesize($tmpName)); 
    $content3 = addslashes($content2); 
    fclose($fp); 

    if(!get_magic_quotes_gpc()) 
    { 
     $fileName = addslashes($fileName); 
    } 

    $query = "INSERT INTO upload (name, size, type, image, date) ". 
     "VALUES ('$fileName', '$fileSize', '$fileType', '$content3', '$filedate')"; 

    mysql_query($query) or die('Error2, query failed'); 
} 

而且我想通過getImage.php檢索圖像頁面稍後通過以下代碼將其用作源頁面,但似乎無法檢索blob數據:

PS圖片上傳成功,但我不能說我最近發佈

getImage.php具體日期檢索:

// Create connection 
$connection = mysql_connect("localhost","root","p206405az"); 

// Check connection 
if (!$connection) { 
    die("Connection failed: " . mysql_error()); 
} 
//select a database to use 
$db_select = mysql_select_db("news" , $connection) ; 
if (!$db_select) { die("Selection faild:" . mysql_error()) ; 
} 

//perform mysql query 

$result = mysql_query("SELECT * FROM news_content" , $connection); 
if (!$result) { 
    die("read failed: " . mysql_error()); 
} 
//useing returned data 
while ($content = mysql_fetch_array($result)){ 

    $date = $content["date"]; 
    $time = $content["time"] ; 

} 

$filedate = "$date" . "$time" ; 

$result2 = mysql_query("SELECT image FROM upload WHERE date='$filedate'" , $connection); 
if (!$result2) { 
    die("read failed: " . mysql_error()); 
}; 

$row = mysql_fetch_assoc($result2); 
mysql_close($connection); 

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

而且我想在的index.php頁面來顯示圖像與此下面的HTML代碼:

的index.php:

<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" /> 

如何檢索getImage.php頁面中的數據,然後使用該頁面az源頁面在index.php頁面中顯示圖像? 任何幫助,將不勝感激。

+0

你得到了什麼錯誤? – fusion3k

+0

@ fusion3k沒事。那就是問題所在。它只是不顯示圖像而是一個破碎的圖像標誌。 –

+0

直接通過url獲取圖片(不是''標籤)和註釋'header()'命令查看錯誤。 – fusion3k

回答

0

無法弄清楚錯誤。但我會將圖像保存爲文件而不是數據庫,並且如果新聞表的行保留其名稱,除非每個新聞有多個圖像。

如果每個新聞有多個圖片,我只會將圖片ID與新聞ID匹配。

架構news.db

id, title, description, content_text, date, time 

架構upload.db:

id, image, newsid 

我的形象的HTML鏈接將是:

<img src="getImage.php?id=<?php echo $newsid; ?>" width="175" height="200" /> 

,然後我getimage.php會是這樣的:

$connection = mysql_connect("localhost","root","p206405az"); 
if (!$connection) { 
    die("Connection failed: " . mysql_error()); 
} 
$db_select = mysql_select_db("images" , $connection) ; 
if (!$db_select) { 
    die("Selection faild:" . mysql_error()) ; 
} 
$id=$_GET["id"]; 

$result = mysql_query("SELECT image FROM upload WHERE newsid='$id' LIMIT 1" , $connection); 
if (!$result) { 
    die("read failed: " . mysql_error()); 
}; 

$row = mysql_fetch_assoc($result); 
mysql_close($connection); 

header("Content-type: image/jpeg"); 
print $row['image']; 
+0

什麼是$ _GET [「id]在這裏做?是否得到表的ID?如果我想讓另一行通過讓說日期列而不是ID,我該怎麼辦?而另一個問題是如果我有在一個數據庫中的3個表,該表的ID爲$ _GET [「id」]得到?第一個表的ID或第二個或第三個? –

+0

這是我提出的解決方案在類似於你的場景。 html鏈接,它會從數據庫中請求具有圖像的特定圖像(上傳表格),然後getimage.php將返回該圖像(從上傳表格)以HTML格式顯示。 – Ranhot

0

來顯示錯誤:

  1. 在頁面頂部添加error_reporting(E_ALL); ini_set('display_errors', '1');;

  2. 暫時評論標題行:header("Content-type: image/jpeg");;

  3. 直接從瀏覽器地址欄中檢索圖片(見下圖);

  4. 如果您獲得「500服務器錯誤」,請檢查Apache錯誤日誌(即使啓用了error_reporting,編譯錯誤也不會顯示);除非PHP引發錯誤

enter image description here

+0

它只會給我這個錯誤:圖像「http:// localhost:89/-C/wamp/www/news/getImage.php?date = 2/13/201603:19:19am「無法顯示,因爲它包含錯誤。我應該將日期列設置爲主鍵嗎? –

+0

1)使用斜槓作爲分隔符不是一個好主意; 2)你有評論'header'行?也請注意以下行,看看是否**文本**打印出來**之前**圖像 – fusion3k

+0

請檢查'$ filedate'變量的值:你確定它匹配'日期'db字段嗎?如果'date'字段是日期時間格式,我認爲它可能不匹配。另外:你要求一個特定的圖像,但是在你的代碼處理過程中,通過url傳遞的'date'是什麼? – fusion3k

相關問題