Benchmarkchart – php example code – part 3 – admin page

Article describe admin page for removing unwanted score with remove.php script. Remove script is invoked by GET url link, then create verification form and submit data for deletion with POST method.

Admin page show all submited score in form of a table. But against way used in index page, there is every row displayed a second row with Manage content leading text and ling for a removal script.

Better way for understanding can be gain after looking on next picture

admin.page content

Code that read data from database table and create this table output is as follows

<?php // code showing all subscribers in form of a table at end of the page
 
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
 
// Check connection
if($dbc === false){
    die(„ERROR: Could not connect to database – stage of article listing. “ . mysqli_connect_error());
}
 
            
// read all rows (data) from guestbook table in „test“ database
$sql = „SELECT FROM benchmark_chart ORDER BY score DESC„;  // read in reverse order of score – highest score first
/**********************************************************************/
/*          Output in Table – solution 1 – for debuging data from database        */
/**********************************************************************/
// if data properly selected from guestbook database tabele
 
echo „<h4>Administration of benchmark result posts</h4>“;
echo „<br>“;
//echo ‚ <button class=“btn btn-secondary btn-lg “ onclick=“location.href=\’unsubscribe.php\'“ type=“button“>  Unsubscribe by e-mail -> </button>‘;
 
echo „<br>“; echo „<br>“;
 
    if($output = mysqli_query($dbc, $sql)){
        if(mysqli_num_rows($output) > 0){  // if any record obtained from SELECT query
            // create table output
            echo „<table>„; //head of table
                echo „<tr>„;
                    echo „<th>id</th>„;
                    echo „<th>score</th>„;
                    echo „<th>nickname</th>„;
                    echo „<th>date of post</th>„;
                    echo „<th>screenshot</th>„;
                 echo „</tr>„;
 
            while($row = mysqli_fetch_array($output)){ //next rows outputed in while loop
                echo “ <div class=\“mailinglist\“> “ ;
                echo „<tr>“;
                    echo „<td>“ . $row[‚id‘] . „</td>“;
                    echo „<td>“ . $row[‚score‘] . „</td>“;
                    echo „<td>“ . $row[‚nickname‘] . „</td>“;
                    echo „<td>“ . $row[‚write_date‘] . „</td>“;
                    $image_location = IMAGE_PATH.$row[‚screenshot‘];
                        echo „<td> <img src=\“$image_location\“ alt=\“ score image \“  height=\“95\“> <td>“; 
                echo „</tr>“;
 
                // removal line with removing link line
                
                echo „<tr>„;
                echo „<td  colspan=\3\„> Manage content: </td>„; 
                                               // description on first line
                echo ‚<td colspan=“2″>
<a id=“DEL“ href=“remove.php?id=‚.$row[‚id‘] . 
&amp;score=‚ . $row[‚score‘] . 
&amp;nickname=‚ . $row[‚nickname‘] 
. ‚&amp;write_date=‚ . $row[‚write_date‘] 
. ‚&amp;screenshot=‚ . $row[‚screenshot‘] .
> DEL – Remove score </a></td></tr>‚; 
//construction of GETable link
                    // for remove.php input
                echo „</tr>“;
 
                echo “ </div> “ ;
            }
            echo „</table>“;
            // Free result set
            mysqli_free_result($output);
        } else{
            echo „There is no benchmark result in chart. Please wirite one.“; // if no records in table
        }
    } else{
        echo „ERROR: Could not able to execute $sql. “ . mysqli_error($dbc); // if database query problem
    }
// Close connection
mysqli_close($dbc);
?>

Content of whole admin page can be obtained from github here.

remove.php script

Remove.php script gather data from GET post (link created by admin page pass GET data into a remove.php script). This script obtain GET data and create internal form for verification of a removal score. This form is next submited a with POST send on themself. But not as self script but calling script name without GET url link (no further GET data need to be obtained again).

After confirmation and setting Yes for deletion, POST data are used for database deletion and removing of submitd score pisture located in images folder.

GUI of confirmation looks like this

Confirmation dialog generated by remove.php script using POST method

Full content of code is shown next

<!– ****************************************************************** –>
<!– PHP „self“ code GET request for remove and POST delete data               –>
<!– ****************************************************************** –>
<!– Vrsion: 1.0        Date: 27.9-3.10.2020 by CDesigner.eu                                  –>
<!– ****************************************************************** –>
<?php  // leading part of page for simple header securing and basic variable setup
    require_once(‚appvars.php‘); // including variables for database
    require_once(‚authorize.php‘); // authorization script for simple header authorization
  // two variables for message and styling of the mesage with bootstrap
  $msg = “;
  $msgClass = “;
  // default values of auxiliary variables
  
?>
<!– ******************************************* –>
<!– script for appropriate scode removal        –>
<!– ******************************************* –>
<!– obtain GET data from admin.php and trough   –>
<!– POST submit remove data from database       –>
<!– ******************************************* –>
<!DOCTYPE html>
<html>
<head>
  <title> Benchmark – admin  </title>
  <link rel=“stylesheet“ href=“./css/bootstrap.min.css“> <!– bootstrap mini.css file –>
  <link rel=“stylesheet“ href=“./css/style.css“> <!– my local.css file –>
    <script src=“https://code.jquery.com/jquery-3.1.1.slim.min.js“ integrity=“sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n“ crossorigin=“anonymous“></script>
        <script src=“https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js“ integrity=“sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb“ crossorigin=“anonymous“></script>
  
</head>
<body>
  <nav class=“navbar navbar-default“>
      <div class=“container“>
        <div class=“navbar-header“>    
          <a class=“navbar-brand“ href=“admin.php“> 3dmark results chart v 1.0 – part for selected score removal </a>
          <a class=“navbar-brand“ href=“index.php“> –> return to main score page</a>
        </div>
      </div>
    </nav>
    <div class=“container“ id=“formcontainer“>  
    
      
    <?php if($msg != “): ?> <!– alert showing part –>
        <div class=“alert <?php echo $msgClass; ?>“><?php echo $msg; ?></div>
      <?php endif; ?> 
       
      <br> <!– logo on the center of the page –>
      <h4>Confirmation of deletion selected score script.</h4>
      <br>
      <br> <!– logo on the center of the page –>
        <img id=“calcimage“ src=“./images/admin.jpg“ alt=“Calc image“ width=“150″ height=“150″>
      <br>
       
            
      <?php // code for GET info about what to remove and submit removing approval
        if(isset($_GET[‚id‘]) && isset($_GET[‚score‘]) && isset($_GET[‚nickname‘]) && isset($_GET[‚write_date‘]) && isset($_GET[‚screenshot‘]) ){
            // take a data from GET link generated by adminscript
            $id = htmlspecialchars($_GET[‚id‘]);
            $score = htmlspecialchars($_GET[‚score‘]);
            $nickname = htmlspecialchars($_GET[‚nickname‘]);
            $write_date = htmlspecialchars($_GET[‚write_date‘]);
            $screenshot = htmlspecialchars($_GET[‚screenshot‘]);
        } else if (isset($_POST[‚id‘]) && isset($_GET[‚score‘]) && isset($_GET[‚nickname‘])) { //grab score from POST – different behavior for removal
            $id = htmlspecialchars($_POST[‚id‘]);
            $score = htmlspecialchars($_POST[‚score‘]);
            $nickname = htmlspecialchars($_POST[‚nickname‘]);
        }  else  { //error info message
            echo ‚<p class=“alert alert-danger“> Please specify any highscore for removal. </p>‘;
        };
        if(isset($_POST[‚submit‘])){
             
            if($_POST[‚confirm‘] == ‚Yes‘ ){ // delete appropriate score post with imagescreenshot
              //delete the screenshotimage from the 
              $id = htmlspecialchars($_POST[‚id‘]);
              $score = htmlspecialchars($_POST[‚score‘]);
              $nickname = htmlspecialchars($_POST[‚nickname‘]);
              $write_date = htmlspecialchars($_POST[‚write_date‘]);
              $screenshot = htmlspecialchars($_POST[‚screenshot‘]);
              @unlink(IMAGE_PATH . $screenshot); // delete image file from the storage
              // conect to the database
              $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
              //Delete score data from the database
              $sql = „DELETE FROM benchmark_chart WHERE id = $id LIMIT 1″;
              // execute SQL
              mysqli_query($dbc, $sql);
              // close database connection
              mysqli_close($dbc);
              // confirm executed command
              echo ‚<p> The highscore of <strong>‘ . $score . ‚</strong> for <strong>‘ . $nickname . ‚</strong> was succesfully removed. </p>‘;
           
            } else {
                echo  ‚<p class=“alert alert-danger“ > The highscore was not removed. </p>‘; 
            }
        } else if (isset($id) && isset($nickname) && isset($write_date) && isset($score) && isset($screenshot)) {
            echo ‚<h5>Are you sure to delete the next highscore? </h5>‘; 
            // show short describtion of score for deletion
            echo ‚<p> <strong> ID: </strong> ‚ . $id .  ‚<br> <strong> Nickname: </strong>‘ . $nickname .
                 ‚<br> <strong> Date: </strong>‘ . $write_date .  
                 ‚<br> <strong> Score: </strong>‘ . $score .'</p>‘; 
              
            //show submited score image for deletion      
            $image_location = IMAGE_PATH.$screenshot; // supplementary construct of image path location
            echo „<img src=\“$image_location\“ alt=\“ score image to delete \“  height=\“95\“>“; 
            echo ‚<br><br>‘;
            echo ‚<form method=“POST“ action=“remove.php“>‘;   //not self but direct this script remove.php – we dont want include any GET data tahat previously send
            echo ‚<input type=“radio“ name=“confirm“ value=“Yes“ /> Yes   ‚; 
            echo ‚<input type=“radio“ name=“confirm“ value=“No“ checked=“checked“ /> No <br><br>‘;  
            
            echo ‚<input type=“hidden“ name=“id“ value=“‚.$id.'“  />‘; 
            echo ‚<input type=“hidden“  name=“nickname“ value=“‚.$nickname.'“  />‘;
            echo ‚<input type=“hidden“ name=“score“ value=“‚.$score.'“ />‘; 
            echo ‚<input type=“hidden“ name=“screenshot“ value=“‚.$screenshot.'“ />‘;
            echo ‚<input type=“hidden“ name=“write_date“ value=“‚.$write_date.'“ />‘;
            echo ‚<input type=“submit“ class=“btn btn-danger“ value=“submit“ name=“submit“ />‘; 
            echo ‚</form>‘; 
                
        };
        echo ‚<br><br>‘;
        echo  ‚<p> <a href = „admin.php“> &lt;&lt Back to admin page. </a></p>‘;
?>
    
    
    
    </div>
          
    
    
     <div class=“footer“> 
          <a class=“navbar-brand“ href=“https://cdesigner.eu“> Visit us on CDesigner.eu </a>
    </div>
    
      
</body>
</html>

After sending Yes or No for deletion, there will be shown two different outputs.

Output after chosing No in confirmation page
Output after chosing Yes in confirmation page – with further verification data of succesfull operation

All output pages contain link for return on a admin.php.

Full code location

Full code of application with all updates can be obtained from github here.