Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery和php将数据与上载图像一起保存到mysql中_Php_Jquery_Mysql - Fatal编程技术网

使用jquery和php将数据与上载图像一起保存到mysql中

使用jquery和php将数据与上载图像一起保存到mysql中,php,jquery,mysql,Php,Jquery,Mysql,这是我第一次来这里 我目前正在创建一个表单,将数据字符串插入mysql数据库,并一起上传一个图像。图像名称必须保存在mysql数据库中,并且文件必须移动到一个文件夹中,我们将其命名为“Image/”文件夹 我使用jquery插入数据。下面是我尝试过的一段代码: 表格: 标题: 内容: 图片: jQuery用于插入: <script> $(document).ready(function(){ $('#btnSave').on('click', function(){

这是我第一次来这里

我目前正在创建一个表单,将数据字符串插入mysql数据库,并一起上传一个图像。图像名称必须保存在mysql数据库中,并且文件必须移动到一个文件夹中,我们将其命名为“Image/”文件夹

我使用jquery插入数据。下面是我尝试过的一段代码:

表格:


标题:
内容:
图片:

jQuery用于插入:

<script>
$(document).ready(function(){
  $('#btnSave').on('click', function(){
    var title = $("#title").val();
    var content = $("#content").val();
    var img = $("#img").val();
    var dataContent = 'title='+title+'&content='+content+"&img="+img;
    if(!title){
        alert("You must fill the title!");
          $("#title").focus();
    }
    else {
        $.ajax({
            type: "post",
            url: 'includes/saveContent.php',
            data: dataContent,
            beforeSend: function() {
                respon.contentColumn.append(respon.loader);   //just loader before saving the data
            },
            success: function(datas) {
                respon.contentColumn.find(respon.loader).remove();
            }
        });
    }
  });
});

</script>

$(文档).ready(函数(){
$('#btnSave')。在('click',function()上{
var title=$(“#title”).val();
var content=$(“#content”).val();
var img=$(“#img”).val();
var dataContent='title='+title+'&content='+content+“&img=“+img;
如果(!标题){
警告(“您必须填写标题!”);
$(“#title”).focus();
}
否则{
$.ajax({
类型:“post”,
url:'includes/saveContent.php',
数据:数据内容,
beforeSend:function(){
respon.contentColumn.append(respon.loader);//在保存数据之前只需加载程序
},
成功:功能(数据){
respon.contentColumn.find(respon.loader.remove();
}
});
}
});
});
saveContent.php文件:

<?php

$title = $_POST['title'];
$content  = $_POST['content'];
$img  = $_POST['img'];

$query = "INSERT INTO tbl_content VALUES('','$title','$content','$img')";
mysql_query($query);

?>

使用此选项。

 $targetDir = "D:\image_uploads";
    if(is_array($_FILES)) {
       if(is_uploaded_file($_FILES['img']['tmp_name'])) {
          if(move_uploaded_file($_FILES['img']['tmp_name'],"$targetDir/".$_FILES['img']['name'])) 
          {
             echo "File uploaded successfully";
          }
       }
    }

如何将图像保存在文件夹中,而将路径名保存在php中的数据库中

<?php
include("config.php");

if(isset($_POST['submit'])){

$folder = "upload/";

$imagefile = $_FILES["image"]["name"];

$temp = $_FILES["image"]["tmp_name"] ;

$wer = move_uploaded_file($temp,"$folder".$imagefile );

$query1="INSERT INTO employee (image) VALUES ('".$imagefile."')";
mysqli_query($conn,$query1);


}

?>
<!DOCTYPE html>

<html> 

<head>

<title>Demo</title>

<style type="text/css">

input[type=file]

{

width: 50%;

padding: 12px 20px;

margin: 8px 0;

display: inline-block;

border: 1px solid #ccc;

border-radius: 2px;

box-sizing: border-box;
}


div {

border-radius: 5px;

background-color: #f2f2f2;

padding: 20px;

}

</style>

<form  method="post"  enctype="multipart/form-data">

<div class="form-group">

        <label for="password" class="form-group">upload Image</label>

    <input id="image" type="file" name="image"    required="required">

     </div>


<input type="submit" class="btn btn-default"  value="register" id="submit" 
name="submit">

        </form>

</body> 

</html>

我只想指出两点1)不要使用mysql!它已被弃用。使用PDO准备的语句或mysqli。此外,在将输入输入到数据库之前,请确保对输入进行清理!
<?php
include("config.php");

if(isset($_POST['submit'])){

$folder = "upload/";

$imagefile = $_FILES["image"]["name"];

$temp = $_FILES["image"]["tmp_name"] ;

$wer = move_uploaded_file($temp,"$folder".$imagefile );

$query1="INSERT INTO employee (image) VALUES ('".$imagefile."')";
mysqli_query($conn,$query1);


}

?>
<!DOCTYPE html>

<html> 

<head>

<title>Demo</title>

<style type="text/css">

input[type=file]

{

width: 50%;

padding: 12px 20px;

margin: 8px 0;

display: inline-block;

border: 1px solid #ccc;

border-radius: 2px;

box-sizing: border-box;
}


div {

border-radius: 5px;

background-color: #f2f2f2;

padding: 20px;

}

</style>

<form  method="post"  enctype="multipart/form-data">

<div class="form-group">

        <label for="password" class="form-group">upload Image</label>

    <input id="image" type="file" name="image"    required="required">

     </div>


<input type="submit" class="btn btn-default"  value="register" id="submit" 
name="submit">

        </form>

</body> 

</html>