Reputation: 821
I have a input file to select some pdfs and after I select pdfs some text inputs open so I can give a title for each pdf.
So, I have a text input like this:
<input type="text" name="title[]" value=""/>
And then I verify if user fill these inputs like this:
if(isset($_POST['sendForm'])){
foreach ($_POST['title'] as $title) {
if(empty($title)){
echo 'Please give a title';
$ok = false;
}
If user fill all titles of each pdf, I want to upload my pdfs to my folder and insert in database information about uploaded pdfs, and I also want to save in database title of each pdf.
The problem Im having is that IF I upload two pdfs and give to my first pdf title of "test1" and to my second "test2" name, on my database these two pdfs titles are saved with same title, that is always title of my last title pdf, in this case "test".
Im using this code below for this example, do you see what can be wrong here?
if($ok){
if(!empty($_FILES['pdfs']['tmp_name'])){
$pdfs = $_FILES['pdfs'];
$countPdf = count($_FILES['pdfs']['tmp_name']);
$folder = '../../uploads/pdfs_articles/';
for($i=0;$i<$countPdf;$i++){
$ext = substr($pdfs['name'][$i],-3);
$pdfName = $idlast.'-'.$i.time().'.'.$ext;
if($pdfs['type'][$i] == 'application/pdf'){
//here I want to get all values of my inputs with name title[]
// so I can insert on database title of each pdf
foreach ($_POST['title'] as $title) {
$pdf_title = $title;
}
$insPdf = $pdo->prepare("INSERT INTO pdfs_articles (pdf, article_id, title) VALUES (?,?,?)");
$insPdf->bindParam(1,$pdfName);
$insPdf->bindParam(2,$idlast);
$insPdf->bindParam(3,$pdf_title);
$insPdf->execute();
move_uploaded_file($pdfs['tmp_name'][$i], $path.$pdfName);
}
}
}
}
Upvotes: 0
Views: 36
Reputation: 78994
I think you need to replace this:
foreach ($_POST['title'] as $title) {
$pdf_title = $title;
}
With this:
$pdf_title = $_POST['title'][$i];
Upvotes: 1
Reputation: 219834
You are looping through all of your titles leaving $pdf_title
with the last value. Then you are actually handling your uploaded PDFs.
Get rid of that loop and access the $_POST['title']
array just like you do your uploaded files:
$insPdf = $pdo->prepare("INSERT INTO pdfs_articles (pdf, article_id, title) VALUES (?,?,?)");
$insPdf->bindParam(1,$pdfName);
$insPdf->bindParam(2,$idlast);
$insPdf->bindParam(3,$_POST['title'][$i]);
$insPdf->execute();
move_uploaded_file($pdfs['tmp_name'][$i], $path.$pdfName);
Upvotes: 1