2017-04-16 80 views
-1

我一直在爲此工作了四個小時,但仍然沒有任何線索,所以我來到這裏尋求幫助。我正在學習PHP和mySQL,我學習的方式之一就是從開源項目中學習。今天我想了解一個開源項目,並且遇到一些問題。這裏是鏈接到開源項目:https://github.com/markpytel/Printstagram基本上,它具有類似於以下,讓我們把它profileinfo.php(該文件的名稱是相當欺騙,它是顯示了不同的用戶上傳的圖像頁)php mySQL如何顯示圖像,IMG src上的問題

<?php 
$sql="SELECT pid,poster, pdate FROM photo WHERE poster='$myusername' OR pid 
in (select pid from tag where taggee='$myusername') OR pid in (select pid 
from ingroup natural join shared where username='$myusername' and username 
!= ownername) ORDER BY pdate desc"; 
$result=mysql_query($sql); 


while($row = mysql_fetch_array($result)) 
{ 
    echo "<hr>"; 
    echo "Posted by: " . $row["poster"]. " &nbsp;&nbsp;&nbsp;Time: " . $row["pdate"]."<br>"; 
    // pid is each photo's unique id in the database 
    $pidrow=($row["pid"]); 

    ?> 
    </head> 
    <body> 

    <form action="listsingleREV.php?pidrow=<?php echo $pidrow; ?>" method="POST"> 
     <input type="submit" id="pidrow" value="View this image" /> 
    </form> 
    </body> 
    </html> 

如果您點擊「查看此圖像」按鈕,用戶上傳的圖像將會出現。我的第一個問題是:圖像src中問號的含義是什麼。我知道img src中有一個PHP標記,但我不明白爲什麼在listsingleREV.phppidrow之間有一個問號。

listsingleREV.php看起來是這樣的:

<?php 
session_start();  
$pidrow=$_GET['pidrow']; 

$conn = mysql_connect("localhost", "root", ""); 
mysql_select_db("printstagram"); 
$sql = "SELECT pid FROM photo WHERE pid=$pidrow"; 
$result = mysql_query($sql); 
?> 
<?php 
while($row = mysql_fetch_array($result)) { 
    ?> 
    <img src="imageview.php?pid=<?php echo $row["pid"]; ?>" /><br/> 
    <?php 
} 
?> 

imageview.php看起來是這樣的:

<?php 

$conn = mysql_connect("localhost", "root", ""); 
mysql_select_db("printstagram") or die(mysql_error()); 
if(isset($_GET['pid'])) { 
    $sql = "SELECT image FROM photo WHERE pid=" . $_GET['pid']; 
    $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error()); 
    $row = mysql_fetch_array($result); 
    header("content-type: image/jpeg"); 
    echo $row["image"]; 
} 
mysql_close($conn); 
?> 

當然,如果一個用戶點擊一個按鈕,查看每個圖像,這是非常不方便的。所以我決定給自己一些練習和修改代碼(這是Apache許可證,所以我可以做到這一點),使得圖像將自動呈現在profileinfo.php上,而不需要點擊按鈕。由於imageview.phplistsingleREV.php顯示圖像,我試圖用這兩個文件替換profileinfo.php中的表單。我爲此工作了四個小時而沒有實現我的目標。有人能告訴我在profileinfo.php上顯示圖像的正確方法,無需點擊按鈕嗎?

+0

'imageview.php?pid ='..?這裏的問號有HTTP GET參數。 –

+4

需要注意的是,這個代碼庫可能不是最適合從中學習的代碼。它使用的是從PHP 7中刪除的API,因爲它非常可怕('mysql_ *'),並且散佈着絕對荒謬的[SQL注入漏洞](http://bobby-tables.com/)。如果你把它作爲一個公共站點,它可以在幾秒鐘內被銷燬。請查看[PHP正確方法](http://www.phptherightway.com/)獲取有關要做什麼和要避免什麼的更新最新建議。 – tadman

+0

@tadman我正要評論同樣的事情...... –

回答

1
As it appears in imageview.pho the $row[pid] contain the link of your image in photo table so add this line to profileinfo.php under the while loop in the place you want it to display 
<img src="<?php echo". $row["pid"]."; ?>"> 
+0

while循環之前的SQL語句應該是什麼? profileinfo.php喚起imageview.php和listsingleREV.php,它們有不同的sql語句。 – gravition