3

I wanna update profile picture of user that has logged in to my website. I use ajax, jquery, php to update data, in order to update data without refresh the page. This code just working to upload image into folder, but when I use this code to update image from database, it only sent null into database.

jquery and ajax script

$("#form-ava").on('submit',(function(e) {
    e.preventDefault();
    $.ajax({
        url: "../config.inc/upload.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        beforeSend : function()
        {
            //$("#preview").fadeOut();
            $("#err").fadeOut();
        },
        success: function(data)
        {
            if(data=='invalid')
            {
                // invalid file format.
                $("#err").html("Invalid File !").fadeIn();
            }
            else
            {
                // view uploaded file.
                $("#preview-ava").html(data).fadeIn();
                $("#form-ava")[0].reset();
      $("#hidden-list").hide();
            }
        },
        error: function(e)
        {
            $("#err").html(e).fadeIn();
        }
   });
}));

And it's the php syntax upload.php

<?php
require_once 'koneksi.php';
 if($_POST)
{
  $id_user= $_POST['id_user'];
  $img = $_FILES['image']['name'];
  $tmp = $_FILES['image']['tmp_name'];

$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp'); // valid extensions
$path = '../images/ava/'; // upload directory
  try {

// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

// can upload same image using rand function
$final_image = rand(1000,1000000).$img;

// check's valid format
if(in_array($ext, $valid_extensions))
{
    $path = $path.strtolower($final_image);

    if(move_uploaded_file($tmp,$path))
    {
        echo "<img src='$path' />";

}
else
{
    echo 'invalid';
}
$update = $db_con->prepare("UPDATE tb_users SET image=:img WHERE id_user=:id_user");
$update->bindparam(":id_user", $id_user);
$update->bindparam(":img", $image);
$update->execute();
$count = $update->rowCount();
if($count>0) {
echo "success";
}else {
echo "can't update!";
}
}
}catch(PDOException $e) {
echo $e->getMessage();
}
}
?>

HTML syntax

<form id="form-ava" action="../config.inc/upload.php" method="post" enctype="multipart/form-data">
         <input type="hidden" id="id_user" name="id_user" value="<?php echo $row->id_user;?>">
                <input id="ava-img" type="file" name="image" value="<?php echo $row->image;?>"/>
                    <input id="button" type="submit" value="Simpan" name="update"></br>
         <a href="#" class="btn btn-large btn-success" id="cancel-act"></i> &nbsp; Batal</a>
     </form>
       <div id="err"></div>
Ayuktia
  • 431
  • 3
  • 11

3 Answers3

2
<form id="form-ava" action="../config.inc/upload.php" method="post" enctype="multipart/form-data">
     <input type="hidden" id="id_user" name="id_user" value="<?php echo $row->id_user;?>">
            <input id="ava-img" type="file" name="image" value="<?php echo $row->image;?>"/>
                <input id="button" type="submit" value="Simpan" name="update"></br>
     <a href="#" class="btn btn-large btn-success" id="cancel-act"></i> &nbsp; Batal</a>
 </form>
   <div id="err"></div>


<script type="text/javascript">
$("#form-ava").on('submit',(function(e) {
e.preventDefault();
$.ajax({
    url: "../config.inc/upload.php",
    type: "POST",
    data:  new FormData(this),
    contentType: false,
    cache: false,
    processData:false,
    beforeSend : function()
    {
        //$("#preview").fadeOut();
        $("#err").fadeOut();
    },
    success: function(data)
    {
        if(data=='invalid')
        {
            // invalid file format.
            $("#err").html("Invalid File !").fadeIn();
        }
        else
        {
            // view uploaded file.
            $("#preview-ava").html(data).fadeIn();
            $("#form-ava")[0].reset();
  $("#hidden-list").hide();
        }
    },
    error: function(e)
    {
        $("#err").html(e).fadeIn();
    }
});
}));
</script>
Ayuktia
  • 431
  • 3
  • 11
0

I found two errors in your code in proses.php

<?php include 'koneksi.php'; 
$foto = @$_POST['foto'];
$id = @$_POST['id'];
$update = $db->prepare("UPDATE tb_users SET foto=:foto WHERE id=:id");
$update>bindParam(":id", $id);
$update->bindParam(":foto", $foto);
$update->execute();
if($update->rowCount() > 0) {
  echo "success";
}
?>
Gerson
  • 1
0

the error messages would be very helpful, but at the very least you are missing a - in $update>bindParam(":id", $id);, so it should be $update->bindParam(":id", $id);

you did the same thing again with $update>rowCount() should be $update->rowCount()

update: I see you edited your question to fix those, but still haven't posted the errors you're receiving. Were those tpyos in your question, or are you progressively fixing your code?

still, it looks like you're missing a closing curly brace } in this statement:

if($update>rowCount() > 0) {
  echo "success";
 ?>

also, why are you silencing notices with @$_POST? if those values are empty, then do you really want to be updating the table?

Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167