-1

I have a code that adds to the database and counts one more if the id is repeated the problem is that it is not accepting letters only numbers what to do? And yes I set my id to char of (100)

Below you will see my table and my code

    VisitorID Primária  char(25) utf8mb4_general_ci     
    2   RequestID   int(255)    
    3   city    varchar(255)    utf8mb4_general_ci      
    4   Device  varchar(255)    utf8mb4_general_ci      

    5   LoginsCounter   int(11)


>NOW THE PHP CODE:


<?php
try {
    
    $id="E9Grs8RqVCVFHmFpL7lh34";
    $quantity=1;
    $RequestId=1223;
 
  $pdo = new PDO('mysql:host=localhost;dbname=visitas', $username ="root", $password="");
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $stmt = $pdo->prepare("SELECT * FROM registros WHERE VisitorID =?");
$stmt->execute([$id]); 
$result= $stmt->fetch();

if($result){

   $sql = "UPDATE registros SET acessos=?  WHERE VisitorID=?";
   $stmt= $pdo->prepare($sql);
   $stmt->execute([($quantity+$result['LoginsCounter']),$result['VisitorID']]);

}else{
  $sql = "INSERT INTO registros (VisitorID,RequestID, LoginsCounter) VALUES ($id,$RequestId,$quantity)";
  $stmt= $pdo->prepare($sql);
  $stmt->execute([$id, $quantity]);
}

  echo $stmt->rowCount();
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}
   ?>

0 Answers0