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

Question:

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 https://subinsb.com/uploading-images-using-imgur-api-in-php/

<?php
$user_id = $_GET['user'];
$connection  = new PDO('mysql:host=remotemysql.com;dbname=;charset=utf8mb4',"","");
$connection ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit'])) {
    if(isset($_FILES['img'])){
      $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, 'https://api.imgur.com/3/image.json');
      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);
      $url=$pms['data']['link'];
      //insert into database with a prepared statement
      $stmt = $connection->prepare("UPDATE user SET avatar = '$url' WHERE user_id =  $user_id");
      $stmt->execute(array($user_id));
      var_dump($stmt);
    }
}
?>
<?php
$query = $connection->prepare('SELECT * FROM user WHERE user_id = "'.$user_id.'" ');
$query->execute();

$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>
    </form>
    <?php
}else{?>
    <?php
}
?>

Answer:

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");
$stmt->execute();

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. https://www.php.net/manual/es/mysqli.prepare.php

PS: add this to it:

enctype='multipart/form-data'

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

Scroll to Top