I don't understand why this update script isn't working. It is supposed to get the names of files in the download folder and update the files table in the database. It is running through the code without exiting when it fails to update but still displays "Update Successful". The downloads folder is under the public_html folder where this script runs from.

<?PHP require_once("up_header.php"); ?>
</head>
 <body>

  <div class="container"> 
      <div class="row" style="text-align:center;" >
        <h1>Foxclone Filelist Updating</h1>
      </div>
      <div class="header">
  </div>   

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$php_scripts = '../php/';
include $php_scripts . 'PDO_Connection_Select.php';

if (!$pdo = PDOConnect("foxclone_data"))
{   
    $chk=0;
    echo "Datbase Connection Failed";
    exit;
}

function convert_filesize($bytes, $decimals = 2){
  $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
  $factor = floor((strlen($bytes) - 1) / 3);
  return sprintf("%.{$decimals}f", $bytes / pow(1000, $factor)) . @$size[$factor];

  $chk=0;
      $isos = glob('download/foxclone_std*');
      $iso = $isos[count($isos) -1];
      $isoname =  basename($iso);
      $md5file = md5_file($iso);
      $size = filesize($iso);            /*  yields filesize in bytes    */
      $isosize = convert_filesize($size);

      $edges = glob('download/foxclone_edge*.iso');
      $edge = $edges[count($edges) -1];
      $edgename =  basename($edge);
      $md5edge = md5_file($edge);
      $size = filesize($edge);
      $edgesize = convert_filesize($size);

       $pdfs = glob('download/foxcloneV*.pdf');
       $pdf = $pdfs[count($pdfs) -1]; 
       $pdfname = basename($pdf);

       $debs = glob('download/*.deb');
       $deb = $debs[count($debs) - 1];
       $debname =  basename($deb);
       $debmd5 = md5_file($deb);                   
       $size = filesize($deb);
       $debsize = convert_filesize($size);

       $srcs = glob('download/*.tar.gz');
       $src = $srcs[count($srcs) - 1];
       $srcname = basename($src);  
       $srcmd5 = md5_file($src);  
       $size = filesize($src);
       $srcsize = convert_filesize($size);

       $issuess = glob('download/news.pdf');
       $issue = $issuess[count($issuess) -1]; 
       $issname = basename($issue);
}


  if( isset( $isoname ) ) { 

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?,`logtime` = now() WHERE `id` = ?");
        $stmt-> execute($isoname, $md5file, $isosize, 1) ;
        $stmt->execute();
        $count = $stmt->rowCount();
           if($count == 1){
              $cnt = 1;
               }
           else{
              echo('Update Failed');
              exit;
               }
        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?,`logtime` = now() WHERE `id` = ?");
        $stmt-> execute([$edgename, $md5edge, $edgesize, 6]) ;
        if($count == 1){
          $cnt = 1;
           }
        else{
          echo('Update Failed');
          exit;
           }        

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `logtime`=now() WHERE `id` = ?");
        $stmt-> execute([$pdfname, 2]) ;  
        if($count == 1){
          $cnt = 1;
           }
       else{
          echo('Update Failed');
          exit;
           }        

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?, `logtime` = now() WHERE `id` = ?");
        $stmt->execute([$debname, $debmd5,$debsize, 3]) ;   
        if($count == 1){
          $cnt = 1;
           }
       else{
          echo('Update Failed');
          exit;
           }

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?,`filesize` = ?,`logtime` = now() WHERE `id` = ?");
        $stmt->execute([$srcname, $srcmd5, $srcsize,4]) ; 
          if($count == 1){
              $cnt = 1;
               }
           else{
              echo('Update Failed');
              exit;
               }
         $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `logtime`=now() WHERE `id` = ?");
         $stmt-> execute([$issname, 5]) ; 
          if($count == 1){
             $cnt = 1;
             }
          else{
            echo('Update Failed');
            exit;
              }

  }

    if($chk=0)
      echo "Database Update Failed";
    else
      echo "Database Update Successful" ;

Appreciate your help.

Dani AI

Generated

A short diagnostic and practical checklist based on the code and comments from and .

The posted snippet contains several issues that explain the symptoms: large portions of the file-discovery logic appear after a return inside the convert_filesize function (so those variables never exist where the updates run); there are accidental assignments used where comparisons are intended; execute() is being called inconsistently and the $count/rowCount usage is unreliable; and empty glob() results can be indexed without checks. Enabling full error reporting (as suggested) is good, but enabling PDO exceptions and checking statement error info gives clearer DB failures.

Checklist and fixes to make the script robust:

  • Turn on PDO exceptions: set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION so DB errors throw and are catchable (see PDO error handling: https://www.php.net/manual/en/pdo.error-handling.php).
  • Verify control flow: ensure code that builds file lists runs in the intended scope (not after a return inside a helper function).
  • Guard array access from glob() and use end() or check empty() before taking last element (see glob: https://www.php.net/manual/en/function.glob.php).
  • Use execute() correctly (pass a single parameter array) and check its boolean return; use $stmt->errorInfo() for diagnostics (see PDOStatement::execute: https://www.php.net/manual/en/pdostatement.execute.php).
  • Don’t treat rowCount() === 0 as always a failure — an UPDATE that sets identical values can legitimately affect 0 rows.
  • Use absolute paths (__DIR__), small helper functions, and a transaction for the group of updates to make failures atomic and easier to debug.

These steps address the common root causes visible in the thread and will make future debugging far simpler.

Recommended Answers

All 8 Replies

It's hard to know because you aren't spitting out any errors.

Add to the top of your PHP script:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Oh, my mistake. You are spitting out those errors!

Is there any way to know if the database statements aren't executing?
Why not temporarily add an echo statement beneath each MySQL query to make sure that it entered that block of code.

Dani, there's a check of the execution of the sql updates on each one. See lines 78 -84 for example.

But the entire thing is wrapped in

if( isset( $isoname ) )

and I don’t see an error message being spit out if that doesn’t evaluate to true.

Also, everything is wrapped in

function convert_filesize

but I don’t see you calling that function anywhere.

You should put echo statements beneath each SQL query and ensure that code block is even being reached.

I've put echo statements in each update statement and changed If (isset) to If (file_exists). I'm still not getting any error messages or db updates. Here's the current code:

<?PHP require_once("up_header.php"); ?>
</head>
 <body>

  <div class="container"> 
      <div class="row" style="text-align:center;" >
        <h1>Foxclone Filelist Updating</h1>
      </div>
      <div class="header">
  </div>   

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$php_scripts = '../php/';
include $php_scripts . 'PDO_Connection_Select.php';

if (!$pdo = PDOConnect("foxclone_data"))
{   
    $chk=0;
    echo "Datbase Connection Failed";
    exit;
}

function convert_filesize($bytes, $decimals = 2){
  $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
  $factor = floor((strlen($bytes) - 1) / 3);
  return sprintf("%.{$decimals}f", $bytes / pow(1000, $factor)) . @$size[$factor];

  $chk=0;
      $isos = glob('download/foxclone_std*.iso');
      $iso = $isos[count($isos) -1];
      $isoname =  basename($iso);
      $md5file = md5_file($iso);
      $size = filesize($iso);            /*  yields filesize in bytes    */
      $isosize = convert_filesize($size);

      $edges = glob('download/foxclone_edge*.iso');
      $edge = $edges[count($edges) -1];
      $edgename =  basename($edge);
      $md5edge = md5_file($edge);
      $size = filesize($edge);
      $edgesize = convert_filesize($size);

       $pdfs = glob('download/foxcloneV*.pdf');
       $pdf = $pdfs[count($pdfs) -1]; 
       $pdfname = basename($pdf);

       $debs = glob('download/*.deb');
       $deb = $debs[count($debs) - 1];
       $debname =  basename($deb);
       $debmd5 = md5_file($deb);                   
       $size = filesize($deb);
       $debsize = convert_filesize($size);

       $srcs = glob('download/*.tar.gz');
       $src = $srcs[count($srcs) - 1];
       $srcname = basename($src);  
       $srcmd5 = md5_file($src);  
       $size = filesize($src);
       $srcsize = convert_filesize($size);

       $issuess = glob('download/news.pdf');
       $issue = $issuess[count($issuess) -1]; 
       $issname = basename($issue);

  /*  END OF FUNCTION   */  

  if (file_exists ($isoname))  {

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?,`logtime` = now() WHERE `id` = ?");
        $count=$stmt->execute($isoname, $md5file, $isosize, 1) ;
        if($count !== 1) {
          echo('Update STD Failed');
         exit;
         }

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?,`logtime` = now() WHERE `id` = ?");
        $count=$stmt-> execute([$edgename, $md5edge, $edgesize, 6]) ;
        if($count !== 1) {
          echo('Update EDGE Failed');
         exit;
         }

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `logtime`=now() WHERE `id` = ?");
        $count=$stmt-> execute([$pdfname, 2]) ;  
        if($count !== 1) {
          echo('Update PDF Failed');
         exit;
         }

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?, `filesize` = ?, `logtime` = now() WHERE `id` = ?");
        $count=$count=$stmt->execute([$debname, $debmd5,$debsize, 3]) ;   
        if($count !== 1) {
          echo('Update DEB Failed');
         exit;
         }

        $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `md5` = ?,`filesize` = ?,`logtime` = now() WHERE `id` = ?");
        $stmt->execute([$srcname, $srcmd5, $srcsize,4]) ; 
        if($count !== 1) {
          echo('Update SRC Failed');
         exit;
         }

         $stmt = $pdo->prepare("UPDATE `files` SET `filename` = ?, `logtime`=now() WHERE `id` = ?");
         $count=$stmt-> execute([$issname, 5]) ; 
          if($count !== 1) {
             echo('Update NEWS Failed');
            exit;
            }

  // From if(file_exists) statement
       }else {
          echo ("file doesn't exist")    }

Got it fixed. I did so many things, I'm not sure what worked.

I highly suggest that, moving forward, you comment your code as you go so that it’s easier to read and easier to follow.

Your code is a bit of a spaghetti mess, which I’m sure makes it very hard to debug when something like this happens.

Plus, you have things like:

$count=$count=$stmt->execute([$debname, $debmd5,$debsize, 3]) ;

Why do you have count=count? And what is it counting? I noticed you also are performing a lot of calculations without putting in comments why you are doing those calculations or what you are trying to calculate. That makes it very difficult for someone else to review and understand your code, but it probably makes it very difficult for you to debug or remember why you did it that way, if you need to revisit the code in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.