I am new to php. I am uploading multiple files from a form along with other inputs to database. Files complete path and name should be inserted in DB in a single column with commas. How do I do that?
$filename = $_FILES['file']['name'];
$folder = "/var/www/html/PhpProject1/";
for($i=0; $i<count($_FILES['file']['name']);$i++)
{
move_uploaded_file($_FILES['file']['tmp_name'][$i], $folder.$_FILES['file']['name'][$i]);
}
$stmt = $conn->prepare("INSERT INTO studentrecords(name, email, mobileno,address,gender,filename) values (?,?,?,?,?,?)");
$stmt->bind_param("ssssss",$name,$email,$mobno,$address,$gender,$filename);
$stmt->execute();
echo "Successfull";
$stmt->close();
$conn->close();
You need to keep the paths in an array, because you are uploading multiple files. So here i am using $paths
array to store the paths. And in the insert query i am using implode
function to convert array to string (with comma). This way you can store all paths as a comma separated value in a single column.
This is your solution
$filename = $_FILES['file']['name'];
$folder = "/var/www/html/PhpProject1/";
$paths = array();
for($i=0; $i<count($_FILES['file']['name']);$i++)
{
$paths[] = $folder.$_FILES['file']['name'][$i];
move_uploaded_file($_FILES['file']['tmp_name'][$i], $folder.$_FILES['file']['name'][$i]);
}
$stmt = $conn->prepare("INSERT INTO studentrecords(name, email, mobileno,address,gender,filename) values (?,?,?,?,?,?)");
$stmt->bind_param("ssssss",$name,$email,$mobno,$address,$gender,implode(",",$paths));
$stmt->execute();
echo "Successfull";
$stmt->close();
$conn->close();