如何上传多个图像并插入mysql的同一行

时间:2019-02-02 14:02:21

标签: php mysql

我希望从用户上传两个或多个证书图像,并将其存储在mysql行中。我该怎么做...

这是我用来上传单张图片的代码...但是如何修改代码以允许存储两张图片

我的addstudent.php文件具有

cd /path/to/my/local/repo
git config user.email

并且我的SaveStudent.php包含

        <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>

现在,我如何在addstudent.php和savestudent.php中添加一个或多个输入来上传图像。

2 个答案:

答案 0 :(得分:1)

HTML就像

<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>

要在多行中插入数据

$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");
}

要在单行中插入数据(逗号分隔)

$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");
}

答案 1 :(得分:0)

foreach (DataGridViewRow row in dataGridView1.Rows) { // Avoid for Row which are currently in Edit mode if (!row.IsNewRow) { row.Visible = row.Index < 2; } } 属性添加到您的输入multiple

将接收到的文件存储为json数组,因此也许您需要更改数据库中的字段类型