gpt4 book ai didi

javascript - $.ajaxFileUpload 成功将文件上传到文件夹,但无法传递要保存在数据库中的值

转载 作者:行者123 更新时间:2023-12-02 18:18:56 24 4
gpt4 key购买 nike

我在提交表单时使用了ajaxfileupload。这也用于上传图像文件。图片已成功上传,但未包含其他要保存到数据库的数据?因为它使我的表字段留空,所以我认为这些值没有传递到category_save.php。我还观察到,当我使用 $.ajax({}) 而不是 $.ajaxFileUpload 时,数据全部成功传递并保存在数据库中,包括图像文件名称,但实际文件根本没有上传。但是当我使用 $.ajaxFileUpload 而不是 $.ajax({}) 时,它只是相反,文件已上传,但值未保存在数据库。这有什么问题吗?这是我的代码:

产品表单.php

<form method="post" name="new_category" id="product_category" enctype="multipart/form-data"> 
<ul class="add_prod">
<li>Category Title:<input type="text" name="category[title]" id="cat_title" value="" placeholder="Title" /> </li>
<li>Category Description:<textarea rows="4" cols="40" name="category[description]"></textarea></li>
<li>Category Image:<input type="file" name="image_file" id="image_file" /></li>
</ul>
</form>

产品1.js

$("#product_category").submit( function(){

event.preventDefault();
var data_category = $(this).serialize();
var image = $("#image_file").val();

$.ajaxFileUpload
(
{
type:"post",
url:"../wp-content/plugins/product_form/category_save.php",
secureuri:false,
fileElementId:'image_file',
dataType: "json",
data:data_category + "&image_file=" +image,
success: function (data)
{
if(data.notify == "Success"){
console.log(data.notify);
}
else{
return false;
}
}
}
);
});

产品2.js

$("#product_category").submit( function(){
event.preventDefault();
var data_category = $(this).serialize();
var image = $("#image_file").val();

$.ajax({
type: "post",
url: "../wp-content/plugins/product_form/category_save.php",
dataType: "json",
data:data_category + "&image_file=" +image,
success: function(data){
if(data.notify == "Success"){
console.log(data.notify);
}
else{
console.log(data.notify);
}
}

});

});

category_save.php

<?php 
//establish connection
$con = mysqli_connect("localhost","root","","ion2_emagi");
//on connection failure, throw an error
if(!$con) {
die('Could not connect: '.mysql_error());
}

$output_dir = "C:/Users/Employees/Dropbox/emagi/wp-content/plugins/product_form/img/";
$file_name = "image_file";

if(isset($_FILES[$file_name]))
{
//Filter the file types , if you want.
if ($_FILES[$file_name]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
//move the uploaded file to uploads folder;
move_uploaded_file($_FILES["image_file"]["tmp_name"],$output_dir. $_FILES["image_file"]["name"]);
}

}

//get the form elements and store them in variables
$category_values = $_POST["category"];
$image_url = basename($_POST["image_file"]);
$image_field = "image_url";
$data = array();

//unset($view_all_info['Password2']);
foreach($category_values as $field => $val){
$data[] = "`".$field."` = '".$val."'";
}

array_push($data,"`".$image_field."` = '".$image_url."'");

$sql = "INSERT INTO wp_product_category SET ".implode(',', $data);
$ret = mysqli_query($con,$sql);

if($ret){
$notification="Success";
}
else{
$notification="Failed";
}

echo json_encode(array('notify'=>$notification));

mysqli_close($con);

最佳答案

data 字段应采用以下格式:

data: { data_category: data_category,  image_file: image_file }

您正尝试将其作为带有参数的 URL 进行传递。

然后您必须在 PHP 中通过参数名称使用 POST 检索它。例如:

$category_values = $_POST["data_category"]; 

关于javascript - $.ajaxFileUpload 成功将文件上传到文件夹,但无法传递要保存在数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19024595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com