【问题标题】:PHP image upload not workingPHP图像上传不起作用
【发布时间】:2013-07-01 14:19:52
【问题描述】:

我只是想上传一张图片并将其位置插入数据库(SQLYog)。该代码在没有数据库连接的情况下工作正常。但是当我尝试将它与 SQL Yog 链接时,操作页面根本不显示任何内容,并且没有任何内容被插入到数据库表中。如果您能提供帮助,那就太好了。这是我的代码。

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 primary key, auto incr) , loc(varchar)

【问题讨论】:

  • 您是否检查过您与数据库的连接是否正常?
  • mysql_query($qu,$con) or die(mysql_error());放上去,这样你在执行查询的时候就可以看到有没有错误。
  • SQLYog 只是 MySQL 的可视化前端(GUI)。您的数据库是 MySQL
  • 谢谢大家的建议和建议 :-)

标签: php image-upload


【解决方案1】:

您必须设置数据库连接变量并检查您的查询如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-22
    • 2018-02-07
    • 2016-02-23
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多