我有一个程序来保存数据库mysql中的数据。但是它也可以保存数据
然而问题是,如果我使用数据保存和单张图片上传它可以正常工作,如果我单独运行,那么多文件上传的代码也可以工作,但不知道为什么两个代码在一起都没有工作。
<form role="form" method ='POST' id='add_user_form' name='add_user_form' enctype="multipart/form-data">
<div class="container margin">
<div class="form-group">
<label for="name">Project Name</label>
<input class="form-control" type="text" id='projName' name='projName' value="" />
</div>
<div class="form-group">
<label for="slug">Project Slug</label>
<input class="form-control" type="text" id='projSlug' name='projSlug' value="" />
</div>
<div class="form-group">
<label for="desc">Project Description</label>
<textarea class="form-control" rows="5" name="projDesc"></textarea>
</div>
<div class="form-group">
<label for="category">Project Category</label>
<select class="form-control" name="projCategory">
<?php
$result = display_Records('categories');
foreach($result as $r){
echo '<option value='.$r["id"].'>'.$r['name'].'</option>';
}
?>
</select>
</div>
<div class="form-group settings">
<label for="endingDate">Display Image</label>
<input type="file" name="fileToUpload" id="fileToUpload">
</div>
<br>
<div>
<input type="file" name="images[]" multiple="multiple" accept="image/*" />
</div>
<br>
<input type='submit' class="btn btn-info btnres" name='btnSubmit' value='SUBMIT'>
</div>
</form>
<?php
session_start();
if(!isset($_SESSION['login'])){ //if login in session is not set
header("Location: index.php");
}
include_once 'includes.php';
include_once 'sidebar.php';
include_once 'functions.php';
include_once 'conn.php';
$extension=array("jpeg","jpg","png","gif");
if(isset($_POST['btnSubmit'])){
$last_id = '';
if(($_FILES["fileToUpload"]["type"] == "image/gif")|| ($_FILES["fileToUpload"]["type"] == "image/jpeg") || ($_FILES["fileToUpload"]["type"] == "image/png") || ($_FILES["fileToUpload"]["type"] == "image/pjpeg")){
$fname = $_FILES['fileToUpload']['name'];
if ($_FILES['fileToUpload']['name'] !=""){
if(!is_dir("uploads/")) {
mkdir("uploads/");
}
$rawBaseName = pathinfo($fname, PATHINFO_FILENAME );
$extension = pathinfo($fname, PATHINFO_EXTENSION );
$counter = 0;
while(file_exists("uploads/".$fname)) {
$fname = $rawBaseName . $counter . '.' . $extension;
$counter++;
};
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],"uploads/".$fname)){
echo 'PHOTOS UPLOADED';
}
else{
echo 'ERROR IN UPLOADING PICTURES';
}
}
}
$value_array = array();
$log_message = 'Added new project';
$value_array['projectName'] = $_POST['projName'];
$value_array['slug'] = $_POST['projSlug'];
$value_array['description'] = $_POST['projDesc'];
$value_array['category'] = $_POST['projCategory'];
$value_array['images'] = $fname;
$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$cols = array();
$vals = array();
foreach ($value_array as $column => $value){
$cols[] = $column;
$vals[] = $value;
}
$colnames = "`".implode("`, `", $cols)."`";
$colvals = "'".implode("', '", $vals)."'";
$sql = "INSERT INTO project (".$colnames.") VALUES (".$colvals.")";
if(mysqli_query($conn, $sql)){
echo 'data saved';
$last_id = mysqli_insert_id($conn);
if(isset($_FILES['images']['name'])):
define ("MAX_SIZE","2000");
for($i=0; $i<count($_FILES['images']['name']); $i++) {
$size=filesize($_FILES['images']['tmp_name'][$i]);
if($size < (MAX_SIZE*1024)):
$path = "uploads/";
$name = $_FILES['images']['name'][$i];
$size = $_FILES['images']['size'][$i];
list($txt, $ext) = explode(".", $name);
date_default_timezone_set ("Asia/Calcutta");
$currentdate=date("d M Y");
$file= time().substr(str_replace(" ", "_", $txt), 0);
$info = pathinfo($file);
$filename = $file.".".$ext;
if(move_uploaded_file($_FILES['images']['tmp_name'][$i], $path.$filename)) :
$fetch=$db->query("INSERT INTO projectimages(project_id, image) VALUES('$last_id' ,'$filename')");
if($fetch):
header('Location:project.php');
else :
$error ='Data not inserting';
endif;
else :
$error = 'File moving unsuccessful';
endif;
else:
$error = 'You have exceeded the size limit!';
endif;
}
else:
$error = 'File not found!';
endif;
}
else{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$('.button').on('click', function() {
$('.content').toggleClass('isOpen');
});
});
</script>