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.

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.