0

我在更新照片的脚本中遇到问题。当我上传照片以更新特定用户时,它会使用我上传的照片更新 mysql 表中的所有用户,而不用staff_id.

根据我的脚本,它假设用 重命名照片,将其staff_id更新给用户并覆盖上传目录中该用户的任何照片。请问我做错了什么,我的片段如下:

<?php
$allowed_filetypes = array('.jpg','.pdf','.xlsx','.xls','.doc','.docx','.ppt','.pptx','.jpeg','.png','.gif','.pdf');
   $max_filesize = 52428800; // max file size = 50MB
   $target = "images/"; 
   $pic=($_FILES['photo']['name']);
 // get form data, making sure it is valid and also sanitize the inputs

 $pic = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));

  //This gets all the other information from the form

$pic=($_FILES['photo']['name']);

    $file = $_FILES['photo']['name']; // Get the name of the file (including file extension).
    $ext = substr($file, strpos($file,'.'), strlen($file)-1);
    if(!in_array($ext,$allowed_filetypes))//check if file type is allowed
        die('The file extension you attempted to upload is not allowed.'); //not allowed
    if(filesize($_FILES['photo']['tmp_name']) > $max_filesize) //check that filesize is less than 50MB
        die ('The file you attempted to upload is too large, compress it below 50MB.');


   // Connects to your Database
     mysql_connect("localhost", "root", "") or die(mysql_error()) ;
     mysql_select_db("office") or die(mysql_error()) ;


$pic=($_FILES['photo']['name']);


$uploaddir = 'images/';

$uploadfile = $uploaddir . basename($_FILES['photo']['name']);

echo "<p>";
if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile)) {

//Writes the information to the database
 mysql_query("UPDATE development SET photo='$pic' ");
 $uploaddir = "images/" .mysql_insert_id() . $ext;  
 $staff_id = mysql_insert_id();
  $new_file_name = mysql_insert_id() . $ext;

  //I removed ,photo='$target' to display only id as picture name
  mysql_query("UPDATE development SET photo='$new_file_name' WHERE staff_id=$staff_id");

echo "File is valid, and was successfully uploaded.\n";

} else {

   echo "Upload failed";

}
echo "</p>";

echo '<pre>';

echo 'Here is some more debugging info:';

print_r($_FILES);

print "</pre>";

?> 
4

3 回答 3

0

你的问题出在这里:

mysql_query("UPDATE development SET photo='$pic' ");

你应该有一个类似的WHERE子句staff_id(以及你所有的更新查询)

mysql_query("UPDATE development SET photo='$pic' WHERE staff_id = ".$staff_id);

* 请注意,现在不推荐使用mysql 扩展,并将在将来的某个时候将其删除。那是因为它很古老,充满了不良做法,缺乏一些现代特征。不要用它来编写新代码。请改用PDOmysqli_*

这是您的查询的 mysqli_ 等效项:

$query = "UPDATE development SET photo='$pic' WHERE staff_id = ".$staff_id

$link = mysqli_connect("[your_host]","[your_user]","[password]","[database]") or die("Error " . mysqli_error($link));

$result = $link->query($query);
于 2013-10-01T06:25:02.227 回答
0

你是第一次更新整个表???为什么???

你的这段代码:

mysql_query("UPDATE development SET photo='$pic' ");

实际上正在更新它。

我认为你需要改进你的代码。

此外,仅在扩展名上检查文件也不好,您可能会被文件添加错误的扩展名所愚弄。

编辑:

您需要从页面获取staff_id,从那里获得更新照片的请求,否则您必须在会话中拥有它或其他东西(因为我不知道它是如何工作的)。

使用该 staff_if 只需添加 WHERE 子句。

而且我不知道这两个更新语句在您的代码中做了什么?

于 2013-10-01T06:25:34.580 回答
0

你读过你的代码吗?

你真的跑了

mysql_query("UPDATE development SET photo='$pic' ");

然后在下面几行,你运行

$staff_id = mysql_insert_id();
$new_file_name = mysql_insert_id() . $ext;
mysql_query("UPDATE development SET photo='$new_file_name' WHERE staff_id=$staff_id");

第一个查询显然会解决您提到的问题。

于 2013-10-01T06:21:20.540 回答