通过javascript代码将图像插入数据库

时间:2014-01-23 06:28:43

标签: javascript php mysql

  

txtfld.php

<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", "root", "") or die(mysql_error());
    $db = mysql_select_db("test1") or die(mysql_error());
    $query = "INSERT INTO image_java ('image') VALUES ('" . addslashes($files) . "')";
    $result = mysql_query($query) or die(mysql_error());
}
}
?>   

当我从堆栈溢出中获取此代码以通过java脚本将图像插入数据库时​​, 但是这里我在第5行的upload-image.php页面中得到这个未定义的变量$ name错误。正如我们所看到的,这里$ name尚未声明,所以我应该在$ name变量中发布什么..所以我想通过此代码或其他任何方式将我的图像插入到数据库中的一个字段任何帮助..谢谢

1 个答案:

答案 0 :(得分:1)

将所有$_FILES[$name]["name"]替换为$_FILES["myInput".$i]["name"]