2014-11-06 74 views
0

我无法在php中更新我的sql数据库中的图像路径。它也没有显示任何错误。我想设置用户的个人资料图片(比如脸书),所以为了我已经完成了编码,但它不起作用,任何人都可以帮助我找到我的错误。无法更新我的数据库中的图像路径

profilepicture.php 
 

 
<!-- Step 3--> 
 

 
<?php 
 

 
<?php 
 
/**********MYSQL Settings****************/ 
 
$host="localhost"; 
 
$databasename="photo_db"; 
 
$user="root"; 
 
$pass=""; 
 
/**********MYSQL Settings****************/ 
 
$conn=mysql_connect($host,$user,$pass); 
 
if($conn) 
 
{ 
 
\t $db_selected = mysql_select_db($databasename, $conn); 
 
\t if (!$db_selected) \t 
 
\t { 
 
\t \t die ('Can\'t use foo : ' . mysql_error()); 
 
\t } 
 
} 
 
else 
 
{ \t die('Not connected : ' . mysql_error()); 
 
} 
 

 

 

 

 
?> 
 

 

 

 

 
    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'; 
 
\t \t default: return false; 
 
\t \t } 
 
\t } 
 

 
\t 
 
\t 
 
\t 
 
if (!empty($_FILES["uploadedimage"]["name"])) 
 
{ 
 
    $file_name=$_FILES["uploadedimage"]["name"]; 
 
    $temp_name=$_FILES["uploadedimage"]["tmp_name"]; 
 
    $imgtype=$_FILES["uploadedimage"]["type"]; 
 
    $ext= GetImageExtension($imgtype); 
 
\t $imagename=$_FILES["uploadedimage"]["name"]; 
 
\t //$imagename=date("y-d-m")."-".time().$ext; 
 
    $target_path = "images/".$imagename; 
 
\t 
 

 
    if(move_uploaded_file($temp_name, $target_path)) 
 
\t { 
 
\t \t 
 
\t \t 
 
\t \t $query_upload="UPDATE signup SET profilepicture='$target_path' WHERE uname='[email protected]' limit 1 " ; 
 
\t \t $qry=mysql_query($query_upload) or die("error in $query_upload == ".mysql_error()); 
 
\t \t if(!$qry) 
 
\t \t { 
 
\t \t \t die("mySQL error: ". mysql_error()); 
 
\t \t } 
 
\t \t else 
 
\t \t { 
 
\t \t \t header("location:index.php"); 
 
\t \t } 
 
\t \t  
 
\t } 
 
\t else 
 
\t { 
 
\t \t exit("Error While uploading image on the server"); 
 
\t } 
 
} 
 
    ?>
index.html 
 

 
<html> 
 
    <head> 
 
<script type="text/javascript"> 
 
function performClick(node) 
 
{ 
 
\t var evt = document.createEvent("MouseEvents"); 
 
\t evt.initEvent("click", true, false); 
 
\t node.dispatchEvent(evt); 
 
\t var theFile = document.getElementById("theFile"); 
 
\t // the file is the first element in the files property 
 

 

 
} 
 
</script> 
 
</head> 
 
    <body> 
 
    
 
     
 

 
     <a href="profilepicture.php" onclick="performClick(document.getElementById('theFile'));">Edit</a> 
 
\t 
 
\t \t \t \t \t \t <input type="file" id="theFile" name="uploadedimage" style="visibility:hidden;" /> 
 
\t \t \t \t \t \t \t 
 
    </body> 
 
</html>

回答

1

添加一些空白在您的SQL字符串:

$query_upload="UPDATE signup". 
    " SET profilepicture='$target_path'". 
    " WHERE uname='[email protected]' limit 1 " ; 

您的查询字符串生成:

UPDATE signupSET profilepicture=$target_pathWHERE uname='[email protected]' limit 1 ; 

而且应该给你一个语法错误。

+0

“更新注册SET profilepicture = $ target_path WHERE uname='[email protected]'limit 1”;这给了我和error :: UPDATE注册错误SET profilepicture = images/Jellyfish.jpg WHERE uname='[email protected]'限制1 =='未知列'图像'在'字段列表'@Jens – 2014-11-06 12:40:19

+0

@namrata已更新我的答案。你必须在'$ target_path'周围加引号,因为它是一个char字段。 – Jens 2014-11-06 12:42:32

+0

通过添加qoutes错误没有提交,但通过选择图像和按下编辑按钮,它给了我除了选择文件按钮“无文件选择”@Jens – 2014-11-06 12:54:34