skip to Main Content

i’m trying to create a multiple photo uploading script in php. All seems to be working fine except the update query. tried updating it manually in phpmyadmin and the problem of not updating persists don’t know what to do can any experts help me solve this problem.

here is the update query:

 try {
          $sql1="update photos 
          set 
          filename='{$db_file_name}',
          upload_date=now() where user='{$_SESSION['id']}' ";
          $st1=$conn->prepare($sql1);
          $st1->execute();

     } 
  catch (Exception $exc) {
          echo $exc->getMessage();   
     }

2

Answers


  1. user is a keyword, better use backticks around it. See: https://dev.mysql.com/doc/refman/8.0/en/keywords.html

     try {
         $sql = "UPDATE `photos` 
                 SET `filename` = :filename, 
                     `upload_date` = NOW() 
                 WHERE `user` = :sess_id";
         $stmt = $conn->prepare($sql);
         $stmt->bindValue(":sess_id", $_SESSION['id']);
         $stmt->bindValue(":filename", $db_file_name);
         $stmt->execute();
    } catch (....) {
       ....
    }
    

    Perhaps better still, don’t use keywords as column names, try userId.

    Login or Signup to reply.
  2. First of all, I would verify again whether all the variables you are using are correct (photos, filename, etc.). i.e. compare them letter by letter with your table. If that looks alright, a little more information wouldn’t be bad. Are you getting any errors? If so, what are they saying? What else have you tried so far?

    Moreover, I would suggest making your code a little easier to read like so:

    /* create a prepared statement */
    if ($st1 = $conn->prepare("UPDATE `photos` SET `filename` = ?, `upload_date` = ? WHERE `user` = ?")) {
    
        /* bind parameters (ssi = string, string, integer)*/
        $st1->bind_param("ssi", $db_file_name, now(), $_SESSION['id']);
    
        /* execute query */
        $st1->execute();
    
        /* close statement */
        $st1->close();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search