The main idea is to create a post along with multiple pictures that associate with the post but the entries for these posts are on a separate table.
I tried making it into one table to do both stuff, but I couldn't find a way to include more than one location for a file in on column.
HTML
<form action="includes/post.inc.php" method="POST" class="col s12" enctype="multipart/form-data">
<div class="row">
<div class="input-field col s12">
<input id="title" name="title" type="text" class="validate" required>
<label for="title">Title</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<textarea id="body" name="body" class="materialize-textarea" required></textarea>
<label for="body">Body</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select name="category" required>
<option value="" disabled selected>Choose a Category</option>
<option value="programming">Programming</option>
</select>
<label>Categories</label>
</div>
</div>
<div class="row">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="file[]" multiple>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Upload one or more files">
</div>
</div>
</div>
<button class="btn waves-effect waves-light right" type="submit" name="post-submit">Post</button>
</form>
PHP
function reArrayFiles($file_post){
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for($i = 0; $i<$file_count; $i++){
foreach($file_keys as $key){
$file_ary[$i][$key] = $file_post[$key][$i];
}
}
return $file_ary;
}
global $idPost;
if (isset($_POST['post-submit'])) {
session_start();
$category = $_POST['category'];
$title = $_POST['title'];
$body = $_POST['body'];
$category = $_POST['category'];
$id = $_SESSION['userId'];
$uid = $_SESSION['userUid'];
$status;
if(empty($title) || empty($body)){
header("Location: ../index.php?error=emptyfields&title=".$title."&mail=".$body);
exit();
}else{
$sql = "INSERT INTO posts (titlePosts, contentPosts, catPosts, timePosts, statusPosts, uidUsers, idUsers) VALUES (?, ?, ?, NOW(), ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../index.php?error=sqlerror");
exit();
}else{
mysqli_stmt_bind_param($stmt, "ssssss", $title, $body, $category, $status, $uid, $id);
mysqli_stmt_execute($stmt);
}
}
$file_array = reArrayFiles($_FILES['file']);
$result = mysqli_query($conn, "SELECT idPosts FROM posts WHERE titlePosts={$title} AND bodyPosts={$body} AND idUsers={$id}");
while($row = mysqli_fetch_assoc($result)){
$idPost = $row['idPosts'];
}
for($i = 0; $i<count($file_array); $i++){
$fileName = $file_array[$i]['name'];
$fileTempName = $file_array[$i]['tmp_name'];
$fileSize = $file_array[$i]['size'];
$fileError = $file_array[$i]['error'];
$fileType = $file_array[$i]['type'];
$fileExt = explode('.',$fileName);
$fileActualExt = strtolower(end($fileExt));
$allow = array('jpg','jpeg','png','JPG','JPEG','PNG');
if(in_array($fileActualExt, $allow)){
if($fileError === 0){
if($fileSize < 100000){
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = '../uploads/'.$fileNameNew;
move_uploaded_file($fileTempName,$fileDestination);
mysqli_query($conn, "INSERT INTO post_files (locationFiles, idPosts) VALUES ({$fileNameNew},{$idPost})");
}else{
echo "<script>alert('Your file is too big!');</script>";
}
}else{
echo "<script>alert('There was an error uploading your file!');</script>";
}
}else{
echo "<script>alert('You cannot upload a file of this type!');</script>";
}
}
if(!isset($category)){
header("Location: ../index.php?post=success");
exit();
}else{
header("Location: ../index.php?cat={$category}&post=success");
exit();
}
}else{
header("Location: ../index.php");
exit();
}
post_files Table
|---------------------|------------------|------------------|
| idFiles | locationFiles | idPosts |
|---------------------|------------------|------------------|
| int | varchar(255) | int |
|---------------------|------------------|------------------|
idPosts
is linked with the posts table
I expected the program to create a post along with the data from the fields, and for the files to be uploaded to htdocs and the file to be inserted into the data base. However, everything goes well except for entering data into the post_files Table.