2013-07-01 83 views
-3

我只是想上傳一個圖像,並將其位置插入數據庫(SQLYog)。代碼工作正常,沒有數據庫連接。但是,當我嘗試將它與SQL Yog鏈接時,操作頁面根本不顯示任何內容,也沒有任何內容插入到數據庫表格中。如果你能提供幫助,那會很好。這裏是我的代碼。PHP的圖像上傳不起作用

form.php的

<html> 
<head> 
<script type="text/javascript"> 
function validate(){ 
var filevalue=document.getElementById("file").value; 
var description=document.getElementById("description").value; 
if(filevalue=="" || filevalue.length<1){ 
alert("Select File."); 
document.getElementById("file").focus(); 
return false; 
} 
if(description=="" || description.length<1){ 
alert("File Description must not be blank."); 
document.getElementById("description").focus(); 
return false; 
} 

return true; 
} 
</script> 
</head> 
<body > 
<h2 align="center" >File Upload</h2> 
<form action="file_upload.php" method="post" 
enctype="multipart/form-data" onSubmit="return validate()" > 
<table align="center" > 
<tr> 
<td><label for="file">File:</label></td> 
<td><input type="file" name="file" id="file" /></td> 
</tr> 
<tr> 
<td><label >File Description:</label></td> 
<td><input type="text" name="description" id="description" /></td> 
</tr> 
<tr> 
<td></td> 
<td><input type="submit" name="submit" value="Submit" /></td> 
</tr> 
<table> 
</form> 
</body> 
</html> 

file_upload.php

<?php 
include("connect.php"); //database connection 
if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/pjpeg")) 
&& ($_FILES["file"]["size"] < 1000000)) 
{ 
if ($_FILES["file"]["error"] > 0) 
{ 
echo "File Error : " . $_FILES["file"]["error"] . "<br />"; 
} 
else { 

echo "Upload File Name: " . $_FILES["file"]["name"] . "<br />"; 
echo "File Type: " . $_FILES["file"]["type"] . "<br />"; 
echo "File Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
echo "File Description:: ".$_POST['description']."<br />"; 

if (file_exists("images/".$_FILES["file"]["name"])) 
{ 
echo "<b>".$_FILES["file"]["name"] . " already exists. </b>"; 
}else 
{ 
move_uploaded_file($_FILES["file"]["tmp_name"],"images/". $_FILES["file"]["name"]); 

$loc="images/".$_FILES["file"]["name"]; 
$qu="insert into images.img(loc) values('$loc')"; 
mysql_query($qu,$con); 
?> 
Uploaded File:<br> 
<img src="images/<?php echo $_FILES["file"]["name"]; ?>" alt="Image path Invalid" > 
<?php 
} 
} 
}else 
{ 
echo "Invalid file detail ::<br> file type ::".$_FILES["file"]["type"]." , file size::: ".$_FILES["file"]["size"]; 
} 
?> 
  • 數據庫名稱:圖像
  • 表名:IMG
  • 表字段:imgid(INT拘謹的進制鍵,自動增量),LOC(VARCHAR)
+0

你檢查,如果你的數據庫連接正常? –

+1

put'mysql_query($ qu,$ con)或die(mysql_error());'這樣你可以看到執行查詢時是否有錯誤。 – DevZer0

+1

SQLYog只是** MySQL **的可視前端(GUI)。你的數據庫是** MySQL **。 – deceze

回答

0

您必須設置數據庫連接變量,並檢查您的查詢,如下所示:

$qu="insert into image.img (imgid,loc) values (NULL,'$loc')"; 
+0

非常感謝你的幫助。現在它工作正常。 – JeNy