使用php将图像名称作为csv插入到mysql数据库中

时间:2013-11-18 13:50:27

标签: php jquery mysql

关于php,我是一个彻头彻尾的菜鸟。我正在从ui读取多个图像并向php发送ajax post请求。应将这些图像移动到文件夹中,然后将其作为csv写入数据库。由于文件没有响应,我不知道出了什么问题。这是我的代码:

HTML

<div class='jumbotron'>
                            <h3>New Inventory</h3>
                            <table>
                            <tr><td>Product Name:</td><td><input name='proName' id='proName'  type="text" class="form-control"></td></tr>
                            <tr><td>Description:</td><td><input name='description' id="description" type="text" class="form-control"></td></tr>
                            <tr><td>Price per unit:</td><td><input name='price' id="price" type="text" class="form-control"></td></tr>
                            <tr><td>Quantity:</td><td><input name='quantity' id="quantity" type="text" class="form-control"></td></tr>
                            <tr><td>Colors:</td><td><input name='colors' id="colors" type="text" class="form-control"></td></tr>
                            <tr><td>Size:</td><td><input name='size' id="size" type="text" class="form-control"></td></tr>
                            <tr><td>University:</td><td><select name="uniDD" id="uniDD"></select></td></tr>
                            <tr><td>Organization:</td><td><select name="orgDD" id="orgDD"></select></td></tr>
                            <tr><td>Images:</td><td><input type="file" name="file1" id="file1"></td></tr>
                            <tr><td></td><td><input type="file" name="file2" id="file2"></td></tr>
                            <tr><td></td><td><input type="file" name="file3" id="file3"></td></tr>
                            <tr><td></td><td><input type="file" name="file4" id="file4"></td></tr>
                            <tr><td></td><td><input type="file" name="file5" id="file5"></td></tr>
                            <tr><td></td><td><input type="file" name="file6" id="file6"></td></tr>
                            <tr><td><input class="ui-button" type="button" value="Add to Inventory" onclick="addProducts()"></td><td></td></tr>
                            </table>
                        </div>

AJAX请求

function addProducts() {
        var proName,description,size,colors,uni,org,file1,file2,file3,file4,file5,file6,quantity,price;
        proName=document.getElementById("proName").value;
        description=document.getElementById("description").value;
        size=document.getElementById("size").value;
        colors=document.getElementById("colors").value;
        uni=document.getElementById("uniDD").value;
        org=document.getElementById("orgDD").value;
        price=document.getElementById("price").value;
        quantity=document.getElementById("quantity").value;
        file1=document.getElementById("file1").value;
        file2=document.getElementById("file2").value;
        file3=document.getElementById("file3").value;
        file4=document.getElementById("file4").value;
        file5=document.getElementById("file5").value;
        file6=document.getElementById("file6").value;
        if((proName=="" || description=="" || size=="" || colors=="" || uni=="" || org=="" || price=="" || quantity=="" || file1=="" || file2=="" || file3=="" || file4=="" || file5=="" || file6=="") ||
                (proName==null || description==null || size==null || colors==null || uni==null || org==null || price==null || quantity==null || file1==null || file2==null || file3==null || file4==null || file5==null || file6==null)) {
            alert("All fields have not been entered correctly!");
        }
        else {
            $.ajax({
                url : "inventory.php",
                type : "POST",
                dataType : 'json',
                data : {
                    proName:proName,
                    description:description,
                    size:size,
                    colors:colors,
                    uni:uni,
                    org:org,
                    file1:file1,
                    file2:file2,
                    file3:file3,
                    file4:file4,
                    file5:file5,
                    file6:file6,
                    quantity:quantity,
                    price:price
                },
                success : function(data) {
                    $(data).each(function(i, val) {
                        console.log(i + " : " + val.res);
                        if(val.res==1) {
                            alert("Record Successfully inserted!");
                            document.admForm.reset();
                        }
                        else {
                            alert("Some Issue has occured. Please try after some time.");
                            //$('#register').dialog('close');
                        }
                    });
                }
            });
        }
    }

PHP

<?php
// order.php
include_once 'functions.php';

$allowedExts = array (
        "gif",
        "jpeg",
        "jpg",
        "png" 
);
$flag=0;
 foreach( $_FILES as $file ) {
    $img+= $file ["name"].',';
    $temp = explode ( ".",$file ["name"] );
    $extension = end ( $temp );
if ((($file["type"] == "image/gif") || ($file["type"] == "image/jpeg") || ($file["type"] == "image/jpg") || ($file["type"] == "image/pjpeg") || ($file["type"] == "image/x-png") || ($file["type"] == "image/png")) && in_array ( $extension, $allowedExts )) {
    if ($file["error"] > 0) {
        echo json_encode(array("response"=>"-2"));
        $flag=0;
        break;
    } else {
        if (file_exists ( "upload/" . $file["name"] )) {
            echo json_encode(array("response"=>"-3"));
            $flag=0;
            break;
        } else {
            move_uploaded_file ($file["tmp_name"], "upload/" .$file["name"] );
            $flag=1;
        }
    }
} else {
    echo json_encode(array("response"=>"-1"));
}
}
if($flag==1) {
$proName=sanitizeString ( $_POST ['proName'] );
$description=sanitizeString ( $_POST ['description'] );
$size=sanitizeString ( $_POST ['size'] );
$colors=sanitizeString ( $_POST ['colors'] );
$uni=sanitizeString ( $_POST ['uni'] );
$org=sanitizeString ( $_POST ['org'] );
$quantity=sanitizeString ( $_POST ['quantity'] );
$price=sanitizeString ( $_POST ['price'] );
    $result = queryMysql ( "INSERT INTO inventory(productName,description,uniID,orgID,colors,size,quantity,cost,images) VALUES('$proName' , '$description' ,'$uni','$org','$colors','$size', '$quantity' , '$price' , '$img')" );
    if ($result) {
        echo json_encode(array("response"=>"1"));
    }
    else {
        echo json_encode(array("response"=>"-4"));
    }
}

?>

感谢所有帮助! 谢谢!

0 个答案:

没有答案