使用javascript代码将图像插入数据库

时间:2014-01-22 07:32:13

标签: javascript php mysql

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">   </script>
<script>
$(function () {
    var inputName = 'myInput';
    $('select[name=myselect]').change(function () {
        $('.inputs').html('');
        for (var i = 0; i < $(this).val(); i++) {
            var inputs = $('<input />').attr({
                name: inputName + i,
                type: 'file'
            });
            $('.inputs').append($('<label>Input ' + (i + 1) + '</label>')).append(inputs).append('<br />');
        }
    });
});
</script>
</head>
<body>
<select name="myselect">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>
<br />
<div class='inputs'></div>
</body>

这里在我的代码中,浏览选项是根据在下拉菜单中选择的数字打开的..这段代码运行正常..但我想将图像插入数据库到一个字段..所以任何帮助,如何使用正常的插入查询将所选图像插入我的数据库..提前感谢..

1 个答案:

答案 0 :(得分:0)

Html代码。

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">   </script>
<script>
$(function () {
    var inputName = 'myInput';
    $('select[name=myselect]').change(function () {
        $('.inputs').html('');

         var forms = $('<form/>').attr({name:'submitBtn',method:'post',id:'upload-image',enctype:"multipart/form-data",action:'upload-image.php'});
         $('.inputs').append(forms);
        for (var i = 0; i < $(this).val(); i++) {
            var inputs = $('<input />').attr({
                name: inputName + i,
                type: 'file'
            });
            $('<label>Input ' + (i + 1) + '</label>').appendTo( "#upload-image" );
            $(inputs).appendTo( "#upload-image" );
            $("<br>").appendTo( "#upload-image" );
        }
        var inputs = $('<input />').attr({name:'total',type: 'hidden',value:$(this).val()});
        $(inputs).appendTo( "#upload-image" );
        var inputs = $('<input />').attr({name:'submitBtn',type: 'submit',value:'upload'});
        $(inputs).appendTo( "#upload-image" );
    });
});
</script>
</head>
<body>
<select name="myselect">
    <option>Select</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>
<br />
<div class='inputs'></div>
</body>

上载image.php

<?php
if (isset($_POST['submitBtn'])) {
    $fileNames = false;
    for ($i = 0; $i < $_POST['total']; $i++) {
        if (!isset($_FILES[$name]["name"]))
            continue;
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $name = 'myInput' . $i;
        $temp = explode(".", $_FILES[$name]["name"]);
        $extension = end($temp);
        if ((($_FILES[$name]["type"] == "image/gif") || ($_FILES[$name]["type"] == "image/jpeg") || ($_FILES[$name]["type"] == "image/jpg") || ($_FILES[$name]["type"] == "image/pjpeg") || ($_FILES[$name]["type"] == "image/x-png") || ($_FILES[$name]["type"] == "image/png")) && ($_FILES[$name]["size"] < 20000) && in_array($extension, $allowedExts)) {
            if ($_FILES[$name]["error"] > 0) {
                echo "Return Code: " . $_FILES[$name]["error"] . "<br>";
            } else {


                if (file_exists("upload/" . $_FILES[$name]["name"])) {
                    echo $_FILES[$name]["name"] . " already exists. ";
                } else {
                    move_uploaded_file($_FILES[$name]["tmp_name"], "upload/" . $_FILES[$name]["name"]);
                    echo "Stored in: " . "upload/" . $_FILES[$name]["name"];
                    $fileNames[] = $_FILES[$name]["name"];
                }
            }
        } else {
            echo "Invalid file";
        }
    }
    if (is_array($fileNames)) {
        $files = implode(",", $fileNames);

        $con = mysql_connect("localhost", "user", "password") or die(mysql_error());
        $db = mysql_select_db("database-name") or die(mysql_error());
        $query = "INSERT INTO pincde ('field_name') VALUES ('" . addslashes($files) . "')";
        $result = mysql_query($query) or die(mysql_error());
    }
}
?>