php – the avatar is not updated by uploading the image to imgur through its api


Hello, I have a problem when submitting the form, it does not upload the image to imgur or update the database, I use this system to load imgur in another code and it works perfectly but I don't know why it doesn't update me here according to what I think is that the form is the problem and it does not load it to imgur the code I removed it from here

$user_id = $_GET['user'];
$connection  = new PDO(';dbname=;charset=utf8mb4',"","");
$connection ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit'])) {
      $image = $_FILES['img']['tmp_name'];
      $client_id=" [api imgur]  ";
      $handle = fopen($image, "r");
      $data = fread($handle, filesize($image));
      $pvars   = array('image' => base64_encode($data));
      $timeout = 30;
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, '');
      curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
      $out = curl_exec($curl);
      curl_close ($curl);
      $pms = json_decode($out,true);
      //insert into database with a prepared statement
      $stmt = $connection->prepare("UPDATE user SET avatar = '$url' WHERE user_id =  $user_id");
$query = $connection->prepare('SELECT * FROM user WHERE user_id = "'.$user_id.'" ');

$result = $query->fetch(PDO::FETCH_ASSOC);
if(isset($result)) {
    <img src="<?php echo $result['avatar']?>">
    <form  method="post" >
        <input style="display: block;" type="file" name="img" class="form-control" id="exampleFormControlFile1" placeholder="Pega urls(.png,jpg,gif)" accept="image/x-png,image/gif,image/jpeg"/>
        <button style="border-radius: 19px 19px 19px 19px;" type="submit" name="submit" class="btn btn-outline-primary postMention" id="postear">Actualizar</button>


You get better if you execute a query instead of a prepare , because you are not using it as it should be, so it does not matter if you use one or the other.

$stmt = $connection->query("UPDATE user SET avatar = '$url' WHERE user_id =  $user_id");

In this way you get the same result, the way you have it is not the correct way to use the prepare, if you want to understand a little more the subject of the prepare here I share with you.

PS: add this to it:


al form as an attribute. It seems that the image is not arriving, place it and tell me.

Scroll to Top