我正嘗試在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頁面中顯示圖像? 任何幫助,將不勝感激。
你得到了什麼錯誤? – fusion3k
@ fusion3k沒事。那就是問題所在。它只是不顯示圖像而是一個破碎的圖像標誌。 –
直接通過url獲取圖片(不是''標籤)和註釋'header()'命令查看錯誤。 – fusion3k