我正在制作一个简单的图片上传表单,其中包含供人们添加评论和其他详细信息的地方。到目前为止,我已经达到了这个代码。
问题:当我运行它时,我上传的文件可以正常进入目录,但表单中的数据没有写入数据库。
<?php
//$con = mysql_connect("localhost","root","");
$con = mysql_connect("localhost","Login","password","DBname");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_fname=$_POST['fname'][$key];
$file_Lname=$_POST['lanem'][$key];
$file_email=$_POST['email'][$key];
$file_school=$_POST['school'][$key];
$file_comment=$_POST['comment'][$key];
$file_name = $key.$_FILES['files']['name'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query = "INSERT INTO sameplTable(`fname`,`lname`,`email`,`school`,`comment`,`filename`) VALUES
('$fname','$lname','$email','$school','$comment','$filename')";
$desired_dir="user_data";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
echo "Success";
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" multiple/>
Value1: <input type="text" name="fname"><br>
Value2: <input type="text" name="Lname"><br>
Value3: <input type="text" name="email"><br>
Value4: <input type="text" name="school"><br>
Value5: <input type="text" name="comment"><br>
<input type="Submit">
</form>