0

I"m looking forward to upload two or more certificates images from user and store in mysql in row. How can I do that...

Here is the code I"m using to upload single image...but How can I modify the code to allow to store two images

My addstudent.php file has

        <span>Name : </span><input type="text" style="width:265px; height:30px;"  name="name" Required /><br>
<span>Cover Letter:</span><input type="file" name="file" id="file"  required ><br><br>

and my SaveStudent.php contains

 <?php
session_start();
include('../connect.php');
$a = $_POST['name'];

 // query

  $file_name  = strtolower($_FILES['file']['name']);
  $file_ext = substr($file_name, strrpos($file_name, '.'));
  $prefix = 'your_site_name_'.md5(time()*rand(1, 9999));
  $file_name_new = $prefix.$file_ext;
  $path = '../uploads/'.$file_name_new;


/* check if the file uploaded successfully */
if(@move_uploaded_file($_FILES['file']['tmp_name'], $path)) {

 //do your write to the database filename and other details   
$sql = "INSERT INTO student (name,file) VALUES (:a,h)";
$q = $db->prepare($sql);
$q->execute(array(':a'=>$a,':h'=>$file_name_new));
header("location: students.php");

}
?>

Now how can I add one or more input to upload images as above in addstudent.php and savestudent.php.

4
  • do you want to save multiple files name in same column 'file' ? Commented Feb 2, 2019 at 15:12
  • and how can I retrive that later to dispaly on image from same column...if it is possible and easy then yes if not consider storing on next column name file_two Commented Feb 2, 2019 at 15:37
  • actually saving all files name in a single column wont be a good approach because at the time of update it will create a mess and also saving in different columns of same row is also not possible because you have to save multiple files and you wont know how many. You should considering saving in multiple rows. If you want to make normalized DB consider making a separate table for it. Commented Feb 2, 2019 at 15:47
  • @JotK.what if I want to save a name in columnt one and image file name in column two under name file_one and second image in column under name file_two of same row..then since above code works. I only need to know how to save a second image..declaring a separate name for second image didn't work for me so...any help? Commented Feb 2, 2019 at 15:54

2 Answers 2

1

Html be like

<span>Name : </span><input type="text" style="width:265px; height:30px;"  name="name" required /><br>
<span>Cover Letter:</span><input type="file" name="file[]" id="file" multiple="" required ><br><br>

To insert data in multiple rows

$a = $_POST['name'];
$sql = "INSERT INTO student (name) VALUES (:a)";
$q = $db->prepare($sql);
$q->execute(array(':a'=>$a));
$id = $q->lastInsertId();

if(count($_FILES)){
    foreach ($_FILES['file']['name'] as $key => $fname) {
        $file_name  = strtolower($fname);
        $file_ext = substr($file_name, strrpos($file_name, '.'));
        $prefix = 'your_site_name_'.md5(time()*rand(1, 9999));
        $file_name_new = $prefix.$file_ext;
        $path = '../uploads/'.$file_name_new;
        if(@move_uploaded_file($_FILES['file']['tmp_name'][$key], $path)) {
            $sql = "INSERT INTO student_cert (student_id,file) VALUES (:id,h)";
            $q = $db->prepare($sql);
            $q->execute(array(':id'=>$id,':h'=>$file_name_new));
        }
    }            
    header("location: students.php");
}

To insert data in single rows (comma seperated)

$a = $_POST['name'];
if(count($_FILES)){
    $file_arr = [];
    foreach ($_FILES['file']['name'] as $key => $fname) {
        $file_name  = strtolower($fname);
        $file_ext = substr($file_name, strrpos($file_name, '.'));
        $prefix = 'your_site_name_'.md5(time()*rand(1, 9999));
        $file_name_new = $prefix.$file_ext;
        $path = '../uploads/'.$file_name_new;
        if(@move_uploaded_file($_FILES['file']['tmp_name'][$key] , $path)) {
            $file_arr[] = $file_name_new;
        }
    }

    if(count($file_arr)) {
        $file_str = implode(', ', $file_arr);
        $sql = "INSERT INTO student (name,file) VALUES (:a,h)";
        $q = $db->prepare($sql);
        $q->execute(array(':a'=>$a,':h'=>$file_str));
    }         
    header("location: students.php");
}
Sign up to request clarification or add additional context in comments.

2 Comments

and How can I display them in image later?
for comma separated method, get the value and explode it, use foreach loop
0

Add multiple attribute to your input <input type="file" name="file" id="file" multiple required>

Store received files as json array, so maybe you need to change field type in DB

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.