PHP Classes

File: examples/addupdateimage.php

Recommend this page to a friend!
  Classes of Barton Phillips   MySQL Slideshow   examples/addupdateimage.php   Download  
File: examples/addupdateimage.php
Role: Example script
Content type: text/plain
Description: Example script
Class: MySQL Slideshow
Present slideshow from images listed in a database
Author: By
Last change: This is a new version of mysqlslideshow. I have removed all of the class/ stuff and included it in the single file examples/mysqlslidehow.class.php.
The class file now has all of the mysqli logic in the class. It also has what was the mysqlslideshow.php logic after the class.
The other file have been modified to work better.
modified: README.md
deleted: class/Error.class.php
deleted: class/SqlException.class.php
deleted: class/dbAbstract.class.php
deleted: class/dbMysqli.class.php
deleted: class/helper-functions.php
deleted: class/mysqlslideshow.class.php
modified: composer.json
deleted: examples/ERROR.log
new file: examples/Pictures/P1010001.JPG
new file: examples/Pictures/P1010002.JPG
new file: examples/Pictures/P1010003.JPG
modified: examples/addimages.php
modified: examples/addupdateimage.php
modified: examples/browserside.html
modified: examples/dbclass.connectinfo.i.php
modified: examples/ie.html
modified: examples/mktable.sql
new file: examples/mysqlslideshow.class.php
deleted: examples/mysqlslideshow.php
modified: examples/serverside.php
Date: 1 year ago
Size: 3,782 bytes
 

Contents

Class file image Download
<?php
// Add, Delete or Update an Image in the Database table

require_once("dbclass.connectinfo.i.php"); // has $Host, $User, $Password

// This file has the MySqlSlideshow class and the GET functions

require_once("mysqlslideshow.class.php"); // $ss is instantiated here!

session_start();

//********************
// Add image
// image: the image path/filename
// subject: optional subject
// description: optional description
// Refresh with $addMessage

if($_POST['submit'] == 'add') {
 
// Add a new image to the table.
 
$image = $_POST['image'];
 
$subject = $_POST['subject'];
 
$desc = $_POST['description'];
 
  if((
$ret = $ss->addImage($image, $subject, $desc)) === true) {
   
$addMessage = "<h3>Image $image Added</h3>";
  } else {
   
$addMessage = "<h3 style='color: red'>Error: $ret</h3>";
  }
 
$_SESSION['add'] = $addMessage;
 
$_SESSION['delete'] = '';
 
$_SESSION['update'] = '';
 
header("Refresh:0; url=addupdateimage.php#add");
}

// Delete image
// id: the id to delete
// Refresh with $delMessage set.

if($_POST['submit'] == 'delete') {
 
$id = $_POST['id'];
  if(
$ss->deleteImage($id) === true) {
   
$delMessage = "<div id='del'><h3>Image with ID=$id Has Been Deleted</h3></div>";
  } else {
   
$delMessage = "<h3>Image with ID=$id Not Found</h3>";
  }
 
$_SESSION['delete'] = $delMessage;
 
$_SESSION['add'] = '';
 
$_SESSION['update'] = '';
 
header("Refresh:0; url=addupdateimage.php#delete");
}

// Update an existing images subject and description
// update: is the id of the image
// subject: optional subject
// description: optional description
// Refresh with $updateMessage set.

if($_POST['submit'] == "update") {
 
$id = $_POST['id'];
 
$subject = $_POST['subject'];
 
$desc = $_POST['description'];
 
  if(
$ss->updateImageInfo($id, $subject, $desc) === true) {
   
$updateMessage = "<h3>Image with id=$id has been updated with<br>subject=$subject,<br>and description=$desc<h3>";
  } else {
   
$updateMessage = "<h3 style='color: red'>$ret</h3>";
  }
 
$_SESSION['update'] = $updateMessage;
 
$_SESSION['delete'] = '';
 
$_SESSION['add'] = '';

 
header("Refresh:0; url=addupdateimage.php#update");
}

$addMessage = $_SESSION['add'];
$delMessage = $_SESSION['delete'];
$updateMessage = $_SESSION['update'];

$current = "No Images<br>";

if(
$ss->query("select id, subject, description, data from mysqlslideshow")) {
 
$current = <<<EOF
<table border="1">
<thead>
<tr><th>ID</th><th>IMAGE</th><th>Subject</th><th>Description</th></tr>
</thead>
<tbody>
EOF;
  while([
$id, $subject, $desc, $data] = $ss->fetchrow('num')) {
   
$current .= "<tr><td>$id</td><td>$data</td><td>$subject</td><td>$desc</td></tr>";
  }
 
$current .= "</tbody>\n</table>";
}

// End of the add and update logic
//********************

echo <<<EOF
<!DOCTYPE html>
<html>
<body>
<h1>Add Image</h1>
<div><h3>Current Images</h3>
$current</div>
<form method='post'>
  <p>Add Image:</p>
  <input type='text' name='image'> image<br>
  <input type='text' name='subject'> subject<br>
  <input type='text' name='description'> description<br>
  <button type='submit' name='submit' value="add">Add Image</button>
</form>
<div id="add">
$addMessage</div>

<h1>Delete an Image Given Image ID</h1>
<form method='post'>
  <p>Delete Image:</p>
  <input type='text' name='id'> ID To Delete<br>
  <button type='submit' name='submit' value="delete">Delete Image</button>
</form>
<div id="delete">
$delMessage</div>
<hr>

<h1>Update Image Given Image ID</h1>
<form method='post'>
  <p>Update Image:</p>
  <input type='text' name='id'> id<br>
  <input type='text' name='subject'> subject<br>
  <input type='text' name='description'> description<br>
  <button type='submit' name='submit' value='update'>Update Image</button>
</form>
<div id="update">
$updateMessage</div>
</body>
</html>
EOF;