i am working on page that should allow the user to add his product for sell, I did make the user add the product information, but didn't know how to allow the user to upload multiple images along with the same time when adding the product informations
here my code so you guys can get a better idea of what i want :
<form action="annonces/addAnnonce.php"method="post"id="addAnnonceForm">
<div class="mb-3">
<label class="form-label">Annonce title</label>
<input type="text" class="form-control"name="title">
</div>
<div class="mb-3">
<label class="form-label">Category</label>
<select class="form-select"name="category" aria-label="Default select example">
<option value="">cars</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Phone / Contact info :</label>
<input type="text" class="form-control"name="phone">
</div>
<div class="mb-3">
<label class="form-label">location:</label>
<select class="form-select"name="location" aria-label="Default select example">
<option value="1">Khartoum</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Annonce images:</label>
<input type="file" class="form-control"name="images[]" multiple>
</div>
<button type="submit" class="btn btn-primary mt-2 mb-3">Add Annonce</button>
And this is the AddAnnonce.php:
function add_new_annonce($title , $category , $location , $uid ) {
global $conn;
if (!empty($title) && !empty($location) && !empty($uid)) {
$n_title = mysqli_real_escape_string( $conn , strip_tags($title));
$n_location = mysqli_real_escape_string( $conn , strip_tags($location));
$publish_date = date('d-m-y');
$n_uid = (int)$uid;
if ($n_uid == 0)
return false;
$query = "INSERT INTO annoncements(title , category ,location , publish_date , uid)
VALUES('$n_title', '$category' , '$n_location', '$publish_date' , '$n_uid' )";
$qresult = mysqli_query($conn , $query);
if (!$qresult)
return false;
if ($qresult)
return true;
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title= $_POST['title'];
$phone = $_POST['phone'];
$location = $_POST['location'];
if (!empty($_POST['title']) && !empty($_POST['phone']) && !empty($_POST['location'])) {
$result = add_new_annonce( "$title","$phone" ,"$location" , $_SESSION['user_info']->user_id );
if ($result) {
echo "success";
}
if (!$result) {
echo "something is wrong";
}
}
}
i do know that i have to create another table annoncements_images and so, but i couldn't find a way how to insert multpile images with multiple images and the annonce_id at that table, how can i get the annonce_id and insert it to the annoncements_images? this is really confusing to me. Please help, and if that wasn't clear please let me know so i can explain more.