0

アップロード フォルダーと mysql データベースの画像を更新しようとしていますが、通常の人物 ID 13.jpg の代わりにファイル名 0.jpg を指定してファイルがアップロードされ、mysql データベースで更新されません。 ?

$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()) ;

    //Writes the information to the 



  $target = "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");


//Writes the file to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

 //Tells you if its all ok
  echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
 echo "Sorry, there was a problem uploading your file.";
}
?>
4

2 に答える 2

0

まず、5.5 で廃止された mysql_* 関数を使用しています。

次に、 mysql_insert_idのマニュアル ページを参照する必要があります。見積もり:

前のクエリ (通常は INSERT) によって AUTO_INCREMENT 列に対して生成された ID を取得します。

これは、users/persons テーブルにデータを挿入または更新した後にのみ mysql_insert_id() を呼び出すことができることを意味します。ただし、あなたの場合、変数$staff_idに格納されている人の ID を既に持っているように見えるため、おそらく mysql_insert_id を使用する必要さえありません。これは代わりに機能しませんか?

$target = "images/" . $staff_id . $ext;
于 2013-09-27T18:24:08.700 に答える