在php中上载和检索图像到数据库

在php中上载和检索图像到数据库,php,mysql,sql,Php,Mysql,Sql,index.php: <html> <head> <title>upload images</title> </head> <body> <form action="index.php" enctype="multipart/form-data" method="post"> <table style="border-collapse: collapse; font: 12px Tahoma;" bord

index.php:

<html>
<head>
<title>upload images</title>
</head>
<body> 
<form action="index.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>

<?php
include("mysqlconnect.php");

    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
     if (!empty($_FILES["uploadedimage"]["name"])) {

    $file_name=$_FILES["uploadedimage"]["name"];
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    $imgtype=$_FILES["uploadedimage"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "images/".$imagename;

if(isset($tmp_name)){
if(move_uploaded_file($tmp_name, $target_path)) {

    $query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES 

('".$target_path."','".date("Y-m-d")."')";
    mysql_query($query_upload) or die("error in $query_upload ==".mysql_error());  

}else{

   exit("Error While uploading image on the server");
} 
}
}
?>
</body>
</html>
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="karma";
$user="root";
$pass="";
/**********MYSQL Settings****************/
$conn=mysql_connect($host,$user,$pass);
if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
    die('Not connected : ' . mysql_error());
}
?>

上传图像
当我运行上述脚本时,没有显示任何错误。数据库中没有不包含的图像

我可以知道如何从数据库上传和检索图像吗

有人能帮我吗


提前感谢

导致代码失败的问题如下:

将代码中
$tmp\u name
的所有实例更改为
$temp\u name

这适用于
if(isset($tmp\u name))
if(move\u upload\u file($tmp\u name,$target\u path))

因为您使用的是
$temp\u name=$\u文件[“uploadedimage”][“tmp\u name”],因此您使用了错误的变量,顺便说一句,该变量未定义

此外,如前所述,在评论区;这一行:

INSERT into 'images_tbl' ('images_path','submission_date')
引用表和列时,不使用引号。要么用背勾将其包裹起来,要么将其移除

例如:

INSERT INTO `tablename` (`column1`,`column2`)

反勾号主要用于防止使用保留字,或者如果使用的字之间正好有空格,或者如果使用连字符作为分词符。(仅供参考)

有关这些保留字的列表,请访问MySQL.com网站:

开发者提示:

<html>
<head>
<title>upload images</title>
</head>
<body> 
<form action="index.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>

<?php
include("mysqlconnect.php");

    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
     if (!empty($_FILES["uploadedimage"]["name"])) {

    $file_name=$_FILES["uploadedimage"]["name"];
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    $imgtype=$_FILES["uploadedimage"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "images/".$imagename;

if(isset($tmp_name)){
if(move_uploaded_file($tmp_name, $target_path)) {

    $query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES 

('".$target_path."','".date("Y-m-d")."')";
    mysql_query($query_upload) or die("error in $query_upload ==".mysql_error());  

}else{

   exit("Error While uploading image on the server");
} 
}
}
?>
</body>
</html>
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="karma";
$user="root";
$pass="";
/**********MYSQL Settings****************/
$conn=mysql_connect($host,$user,$pass);
if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
    die('Not connected : ' . mysql_error());
}
?>
在开发过程中,使用正确的错误报告是至关重要的

如果发现任何错误,将以下内容添加到文件顶部将有助于发出错误信号:

error_reporting(E_ALL);
ini_set('display_errors', 1);
最后:

<html>
<head>
<title>upload images</title>
</head>
<body> 
<form action="index.php" enctype="multipart/form-data" method="post">
<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>
</tr>
<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>
</tbody></table>
</form>

<?php
include("mysqlconnect.php");

    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
     if (!empty($_FILES["uploadedimage"]["name"])) {

    $file_name=$_FILES["uploadedimage"]["name"];
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    $imgtype=$_FILES["uploadedimage"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "images/".$imagename;

if(isset($tmp_name)){
if(move_uploaded_file($tmp_name, $target_path)) {

    $query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES 

('".$target_path."','".date("Y-m-d")."')";
    mysql_query($query_upload) or die("error in $query_upload ==".mysql_error());  

}else{

   exit("Error While uploading image on the server");
} 
}
}
?>
</body>
</html>
<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="karma";
$user="root";
$pass="";
/**********MYSQL Settings****************/
$conn=mysql_connect($host,$user,$pass);
if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
    die('Not connected : ' . mysql_error());
}
?>
您当前的代码对用户开放。使用,或与


可以在这些页面中找到示例。

“当我运行上述脚本时,没有显示任何错误”-您正在检查它们吗?如果没有,请将错误报告添加到文件顶部
错误报告(E_ALL);ini设置(“显示错误”,1)上面的代码将向您抛出一个错误,因为您没有检查错误,而是将表和列视为错误;删除引号。@Fred ii-:抱歉我的错误。。但在那之前我跑了,那一次没有任何错误。。你能解决这个问题吗?
INSERT-into-images\u-tbl(images\u-path,submission\u-date)
而不是
INSERT-into-images\u-tbl'('images\u-path','submission\u-date')
,正如我上面所述。@Fred ii-:现在也没有错误,但没有发生任何事情。。数据库中没有图像。。