使用php将图像名称作为csv插入mysql数据库
说到使用php将图像名称作为csv插入mysql数据库,php,jquery,mysql,Php,Jquery,Mysql,说到php,我是个彻头彻尾的傻瓜。我正在从ui读取多个图像,并向php发出ajax post请求。这些图像应移动到文件夹中,然后作为csv写入数据库。我不知道出了什么问题,因为文件没有回复。这是我的密码: HTML <div class='jumbotron'> <h3>New Inventory</h3> <table>
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>
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"));
}
}
?>
你就是不能发送这样的文件。您需要使用fileReader或构造一个新FormData
对象来传递数据;但是,IE 9及以下版本仍然不支持此操作。我是否应该使用
发送数据?这能解决问题吗?将
放在
之外,然后不必手动构建您拥有的所有数据,只需执行var f=new FormData($('form')[0])代码>。然后,您可以迭代$\u文件
和$\u POST
以按元素的名称
获取元素。我应该通过ajax
手动将表单数据传递给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"));
}
}
?>