我有一個頁面顯示一些圖像(數據庫驅動)。這裏是我的gallery.php代碼:如何上傳圖片並將路徑保存到數據庫?
<ul id="portfolio-list" class="gallery">
<?php
$sql="select * from eikones ";
$res=mysql_query($sql);
$count=mysql_num_rows($res);
for ($i = 0; $i < $count; ++$i)
{
$row = mysql_fetch_array($res);
$co=$i+1;
if(isset($row[ "path" ]))
{
$path= $row[ "path" ];
}
if(isset($row[ "auxon" ]))
{
$auxon = $row[ "auxon" ];
}
if($_SESSION['role'] == "admin")
echo "<li class=\"pink\"><a href=\"$path\" rel=\"group1\" class=\"fancybox\" title=\"Προιόν \"><img src=\"$path\" alt=\"Pic\"></a></li>\n";
}
?>
</ul>
現在我想要一個表單,我將能夠上傳圖像。我想這一點,但它不工作:
<form enctype="multipart/form-data" action="gallery.php" method="post" name="changer">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
<?php
include 'conf.php'; //database connect
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
$tmpName = $_FILES['image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$query = "INSERT INTO eikones"; //table name = "eikones" and it has two columns named "auxon" and "path". The auxon is the id.
$query .= "(image) VALUES ('','$data')";
$results = mysql_query($query, $link) or die(mysql_error());
print "DONE";
}
else {
print "NO IMAGE SELECTED";
}
?>
它說:「未選擇任何圖片」並沒有什麼新的東西都是到數據庫中。
首先,你告訴它插入兩個值當只有指定一列把它們到 –
@scrowler會有幫助:$ insert = mysql_query(「INSERT INTO eikones VALUES('','$ data')」)而不是有2行? –