上传带有甜蜜提醒的图片

时间:2019-03-24 09:23:40

标签: php mysql ajax sweetalert

这是我的甜蜜提醒代码,我在甜蜜提醒中使用了HTML,但我不知道如何更新图片(例如名字等),请帮忙

如何将图像传递到ajax并将其保存在“我的文件和数据库”中

我正在使用SweetAlert进行更新,所以我在sweetalert中使用了HTML

   $('.update').on(' click ', function() {
        swal({
            title: 'Update User',
            text: "You won't be able to revert this!",
            confirmButtonColor: '#28A745',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Update',
            showCancelButton: true,
            reverseButtons: true,
            html: '<input id="swal-input1" class="swal2-input">' +
                '<input id="swal-input2" class="swal2-input">' +
                '<input id="email" class="swal2-input">' +
                '<input id="phone" class="swal2-input">' +
                '<input type="file" accept: "image/*" aria-label: "Upload your profile picture" id="avatar" class="swal2-input">'

        }).then((result) => {
            if (result.value) {

                var firstName = document.getElementById("swal-input1").value;
                var lastName = document.getElementById("swal-input2").value;
                var email = document.getElementById("email").value;
                var phone = document.getElementById("phone").value;
                var avatar = document.getElementById("avatar").src;

                $.ajax({
                    type: 'post',
                    url: 'update.php',
                    data: {
                        'update': true,
                        'u_id': this.id,
                        'first_name': firstName,
                        'last_name': lastName,
                        'email': email,
                        'phone': phone,
                        'avatar': avatar,
                    },
                    success: function(data) {
                        console.log(avatar);
                        swal(" Updated Successfuly ", " User has been Updated . ", "success");
                    },
                    error: function(xhr, thrownError, ajaxOptions) {}
                });
            }
        })
    });
</script>

如何传递图像文件并将其存储并重命名,并检查图像是否在更新时被保存

Update.php

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "smox_db";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    if(isset($_POST['update'])) {
        $id= $_POST['u_id'];
        $first_name= $_POST['first_name'];
        $last_name= $_POST['last_name'];
        $email= $_POST['email'];
        $phone= $_POST['phone'];

            $sql = "UPDATE users SET first_name='$first_name',last_name='$last_name',email='$email',phone='$phone' WHERE id=$id";


    }
    if ($conn->query($sql) === true) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }

    $conn->close();

0 个答案:

没有答案