使用ajax将类型文件输入到php don';行不通

使用ajax将类型文件输入到php don';行不通,php,jquery,ajax,Php,Jquery,Ajax,我使用教程制作了一个CRUD ajax和php,只做了一点修改 我想添加一个图像与其他输入一起上传。 所以我的问题是{name&email&phone}存储的数据跳过了图像。{user_image} 请帮帮我,谢谢你 函数getUsers(){ $.ajax({ 键入:“POST”, url:'crud3/userAction.php', 数据:'action_type=view&'+$(“#userForm”).serialize(), 成功:函数(html){ $('#userDat

我使用教程制作了一个CRUD ajax和php,只做了一点修改

我想添加一个图像与其他输入一起上传。 所以我的问题是{name&email&phone}存储的数据跳过了图像。{user_image}

请帮帮我,谢谢你


函数getUsers(){
$.ajax({
键入:“POST”,
url:'crud3/userAction.php',
数据:'action_type=view&'+$(“#userForm”).serialize(),
成功:函数(html){
$('#userData').html(html);
}
});
}
函数userAction(类型、id){
id=(typeof id=“未定义”)?“”:id;
var statusArr={add:“add”,edit:“updated”,delete:“deleted”};
var userData=“”;
如果(类型==“添加”){
userData=$(“#addForm”).find('.form').serialize()+'&action_type='+type+'&id='+id;
}else if(类型=='edit'){
userData=$(“#editForm”).find('.form').serialize()+'&action_type='+type;
}否则{
userData='action_type='+type+'&id='+id;
}
$.ajax({
键入:“POST”,
url:'crud3/userAction.php',
数据:userForm,
mimeType:“多部分/表单数据”,
contentType:false,
cache:false,
processData:false,
成功:功能(msg){
警报(用户数据);
如果(msg=='ok'){
警报('用户数据已被'+statusArr[type]+'成功');
getUsers();
$('.form')[0].reset();
$('.formData').slideUp();
}否则{
警报('出现问题,请重试');
}
}
});
}
函数editUser(id){
$.ajax({
键入:“POST”,
数据类型:'JSON',
url:'crud3/userAction.php',
数据:'action_type=data&id='+id,
成功:功能(数据){
$('#idEdit').val(data.id);
$('#namedit').val(data.name);
$('#emailEdit').val(data.email);
$('#phoneEdit').val(data.phone);
$('#editForm')。slideDown();
}
});
}

这是userAction.php

<?php
include ('DB.php');
require 'function.php';
$db = new DB();
$tblName = 'users';
if(isset($_POST['action_type']) && !empty($_POST['action_type'])){
    if($_POST['action_type'] == 'data'){
        $conditions['where'] = array('id'=>$_POST['id']);
        $conditions['return_type'] = 'single';
        $user = $db->getRows($tblName,$conditions);
        echo json_encode($user);
    }elseif($_POST['action_type'] == 'view'){
        $users = $db->getRows($tblName,array('order_by'=>'id DESC'));
        if(!empty($users)){


            $count = 0;
            foreach($users as $user): $count++;

                echo '<tr>';
                echo '<td>#'.$count.'</td>';
                echo '<td>'.$user['name'].'</td>';
                echo '<td>'.$user['email'].'</td>';
                echo '<td>'.$user['phone'].'</td>';
                echo '<td>'.$user['img'].'</td>';
                echo '<td><a href="javascript:void(0);" class="c-teal-500 ti-view-list-alt" onclick="editUser(\''.$user['id'].'\')"></a><a href="javascript:void(0);" class="c-blue-500 ti-share" onclick="return confirm(\'Are you sure to delete data?\')?userAction(\'delete\',\''.$user['id'].'\'):false;"></a></td>';
                echo '</tr>';
            endforeach;
        }else{
            echo '<tr><td colspan="5">No user(s) found......</td></tr>';
        }
    }



    elseif($_POST['action_type'] == 'add'){

        $image = '';
        if($_FILES["user_image"]["name"] != '')
        {
            $image = upload_image();
        }

        $userData = array(
            'name' => $_POST['name'],
            'email' => $_POST['email'],
            'phone' => $_POST['phone']
            'img'   => $image

        );
        echo $userData;
        $insert = $db->insert($tblName,$userData);
        echo $insert?'ok':'err';



    }


    elseif($_POST['action_type'] == 'edit'){
        if(!empty($_POST['id'])){


            $userData = array(
                'name' => $_POST['name'],
                'email' => $_POST['email'],
                'phone' => $_POST['phone']

            );
            $condition = array('id' => $_POST['id']);
            $update = $db->update($tblName,$userData,$condition);
            echo $update?'ok':'err';
        }
    }elseif($_POST['action_type'] == 'delete'){
        if(!empty($_POST['id'])){

            $condition = array('id' => $_POST['id']);

            $delete = $db->delete($tblName,$condition);

            echo $delete?'ok':'err';
        }
    }
    exit;
}



?>

这是function.php

<?php

$username = 'root';
$password = '';
$connection = new PDO( 'mysql:host=localhost;dbname=codexworld', $username, $password );

function upload_image()
{
    if(isset($_FILES['user_image']))
    {
        $extension = explode('.', $_FILES['user_image']['name']);
        $new_name = rand() . '.' . $extension[1];
        $destination = './upload/' . $new_name;
        move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
        return $new_name;
    }
}

function get_image_name($id)
{

    $statement = $connection->prepare("SELECT img FROM users WHERE id = '$id'");
    $statement->execute();
    $result = $statement->fetchAll();
    foreach($result as $row)
    {
        return $row["image"];
    }
}



?>
这是DB.php

<?php
/*
 * DB Class
 * This class is used for database related (connect, insert, update, and delete) operations
 * @author  CodexWorld.com
 * @url     http://www.codexworld.com
 * @license http://www.codexworld.com/license
 */
class DB{
    private $dbHost     = "localhost";
    private $dbUsername = "root";
    private $dbPassword = "";
    private $dbName     = "codexworld";

    public function __construct(){
        if(!isset($this->db)){
            // Connect to the database
            $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->db = $conn;
            }
        }
    }

    /*
     * Returns rows from the database based on the conditions
     * @param string name of the table
     * @param array select, where, order_by, limit and return_type conditions
     */
    public function getRows($table,$conditions = array()){
        $sql = 'SELECT ';
        $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
        $sql .= ' FROM '.$table;
        if(array_key_exists("where",$conditions)){
            $sql .= ' WHERE ';
            $i = 0;
            foreach($conditions['where'] as $key => $value){
                $pre = ($i > 0)?' AND ':'';
                $sql .= $pre.$key." = '".$value."'";
                $i++;
            }
        }

        if(array_key_exists("order_by",$conditions)){
            $sql .= ' ORDER BY '.$conditions['order_by']; 
        }

        if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
            $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit']; 
        }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
            $sql .= ' LIMIT '.$conditions['limit']; 
        }

        $result = $this->db->query($sql);

        if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
            switch($conditions['return_type']){
                case 'count':
                    $data = $result->num_rows;
                    break;
                case 'single':
                    $data = $result->fetch_assoc();
                    break;
                default:
                    $data = '';
            }
        }else{
            if($result->num_rows > 0){
                while($row = $result->fetch_assoc()){
                    $data[] = $row;
                }
            }
        }
        return !empty($data)?$data:false;
    }

    /*
     * Insert data into the database
     * @param string name of the table
     * @param array the data for inserting into the table
     */
    public function insert($table,$data){
        if(!empty($data) && is_array($data)){
            $columns = '';
            $values  = '';
            $i = 0;
            if(!array_key_exists('created',$data)){
                $data['created'] = date("Y-m-d H:i:s");
            }
            if(!array_key_exists('modified',$data)){
                $data['modified'] = date("Y-m-d H:i:s");
            }
            foreach($data as $key=>$val){
                $pre = ($i > 0)?', ':'';
                $columns .= $pre.$key;
                $values  .= $pre."'".$val."'";
                $i++;
            }
            $query = "INSERT INTO ".$table." (".$columns.") VALUES (".$values.")";
            $insert = $this->db->query($query);
            return $insert?$this->db->insert_id:false;
        }else{
            return false;
        }
    }

    /*
     * Update data into the database
     * @param string name of the table
     * @param array the data for updating into the table
     * @param array where condition on updating data
     */
    public function update($table,$data,$conditions){
        if(!empty($data) && is_array($data)){
            $colvalSet = '';
            $whereSql = '';
            $i = 0;
            if(!array_key_exists('modified',$data)){
                $data['modified'] = date("Y-m-d H:i:s");
            }
            foreach($data as $key=>$val){
                $pre = ($i > 0)?', ':'';
                $colvalSet .= $pre.$key."='".$val."'";
                $i++;
            }
            if(!empty($conditions)&& is_array($conditions)){
                $whereSql .= ' WHERE ';
                $i = 0;
                foreach($conditions as $key => $value){
                    $pre = ($i > 0)?' AND ':'';
                    $whereSql .= $pre.$key." = '".$value."'";
                    $i++;
                }
            }
            $query = "UPDATE ".$table." SET ".$colvalSet.$whereSql;
            $update = $this->db->query($query);
            return $update?$this->db->affected_rows:false;
        }else{
            return false;
        }
    }

    /*
     * Delete data from the database
     * @param string name of the table
     * @param array where condition on deleting data
     */
    public function delete($table,$conditions){
        $whereSql = '';
        if(!empty($conditions)&& is_array($conditions)){
            $whereSql .= ' WHERE ';
            $i = 0;
            foreach($conditions as $key => $value){
                $pre = ($i > 0)?' AND ':'';
                $whereSql .= $pre.$key." = '".$value."'";
                $i++;
            }
        }
        $query = "DELETE FROM ".$table.$whereSql;
        $delete = $this->db->query($query);
        return $delete?true:false;
    }
}

请将您的代码复制并粘贴到问题中。代码的图像是无用的。请添加您的ajax代码。