如何在PHP中更新图像?

时间:2014-04-25 12:03:25

标签: php mysql

我有一个php页面,将图片更新为db(longblub) 请任何人帮我图像不更新 alt文本更新正常,但其他三个字段没有更新 上面的代码是对的吗?如果没有,请建议我在哪里错了??? 请检查以下代码

<?php
$hostname = "localhost";
$dbuser = "user_e";
$dbpassword = "pwd_e";
$databasename = "test";

$bd = mysql_connect($hostname, $dbuser, $dbpassword) or die("Wrong username or   password.");
mysql_select_db($databasename, $bd) or die("Can't connect to the database");

if ((($_FILES["userFile"]["type"] == "image/gif")
  || ($_FILES["userFile"]["type"] == "image/jpeg")
  || ($_FILES["userFile"]["type"] == "image/png")
  || ($_FILES["userFile"]["type"] == "image/pjpeg"))
  && ($_FILES["userFile"]["size"] < 200000)) {

  if ($_FILES["userFile"]["error"] > 0) {
    echo "Return Code: " . $_FILES["userFile"]["error"] . "<br>";
  }
} 
$type= $_FILES['userFile']['type'];
$userFile= $_FILES['userFile']['name'];
$checkbox = $_POST['checkbox']; 
$countCheck = count($_POST['checkbox']);

for($i=0;$i<$countCheck;$i++) {
  $upd_id  = $checkbox[$i];
  // Temporary file name stored on the server 
  $tmpName  = $_FILES['userFile']['tmp_name'];   
  $imgData = addslashes(file_get_contents($_FILES["userFile"]["tmp_name"]));

  // Read the file 
  $fp   = fopen($tmpName, 'r'); 
  $data = fread($fp, filesize($tmpName)); 
  $data = addslashes($data); 

  fclose($fp); 

  {

  $data = mysql_real_escape_string($data);
  $alt = htmlentities($_POST['altText']);
  echo "upd item ". $upd_id;
  $sql ="UPDATE dtwelcome SET  type='".$_FILES['userFile']  ['type']."', name='.$userFile.',alt='.$alt.',img='.$imgData.' WHERE id= '$upd_id'";
  $result = mysql_query($sql,$bd);  
}

if($result) { 
  echo "success";
  //header('Location: ../../welcome.php');
}
else {
  echo "Error: ".mysql_error();
}
}

?>

谢谢

1 个答案:

答案 0 :(得分:1)

将更新语句更改为以下代码。

$sql ="UPDATE dtwelcome SET  type='{$_FILES['userFile']['type']}', name='{$userFile}',alt='{$alt}',img='{$imgData}' WHERE id= '{$upd_id}'";

我认为你的串联sql字符串是错误的。

避免在DB上保存图像,而不是使用move_uploaded_file在数据库中保存图像,而只保存用于在磁盘上存储图像的路径。