Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Uploading Images to a Page

images php coding issues or problems here.

Moderators: macek, egami, gesf

Uploading Images to a Page

Postby mmarkym » Sat Jun 11, 2011 10:36 am

Hi,

I have a form with an upload field for images to be uploaded to a page. There is an HTML form page with a select statement on it called photoGallery.php

Code: Select all
<?php
   require_once('authorize.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Photo Gallery</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?php

   require_once('appvars.php');
   require_once('connectvars.php');
   
   $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
   
   $query = "SELECT * FROM photogallery WHERE approved = 1";
   
   $result = mysqli_query($dbc, $query);
   
   echo '<table>';
   
   while ($row = mysqli_fetch_array($result))
   {
      echo '<tr><td>' . $row['firstname'] . '</br>' . $row['date'] . '</br>' . $row['description'] . '</td></tr><tr>';
      if (is_file(GW_UPLOADPATH . $row['image']) && filesize(GW_UPLOADPATH . $row['image'] > 0))
      {
         '<td><img src="' . GW_UPLOADPATH . $row['image'] . '" alt="image" /></td></tr>';
    }
    else {
      echo '<td><img src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified" /></td></tr>';
    }
   }
   echo '</table>';

?>

 <hr />
  <form enctype="multipart/form-data" method="post" action="addImages.php">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo GW_MAXFILESIZE; ?>" />
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />
    <label for="description">Description:</label>
    <textarea name="desc" cols="5" rows="5"></textarea>
    <label for="image">Upload Image:</label>
    <input type="file" id="image" name="image" />
    <hr />
    <input type="submit" value="Add" name="submit" />
  </form>


</body>
</html>


There is the addimage.php page that adds the image to the database, which works.

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php
   require_once('appvars.php');
     require_once('connectvars.php');

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  // Retrieve the score data from MySQL
  $query = "SELECT * FROM photogallery ORDER BY date ASC";
  $data = mysqli_query($dbc, $query);

  // Loop through the array of score data, formatting it as HTML
  echo '<table class="dbData">';
  while ($row = mysqli_fetch_array($data)) {
    // Display the score data
    echo '<tr class="scorerow"><td><strong>' . $row['firstname'] . '</strong></td>';
    echo '<td>' . $row['date'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td><a href="removeimage.php?id=' . $row['id'] . '&amp;name=' . $row['firstname'] . '&amp;date=' . $row['date'] .
      '&amp;description=' . $row['description'] . '&amp;image=' . $row['image'] . '">Remove</a></td></tr>';
   if ($row['approved'] == '0')
   {
       echo '<td><a href="approveimages.php?id=' . $row['id'] . '&amp;name=' . $row['firstname'] . '&amp;date=' . $row['date'] .
      '&amp;description=' . $row['description'] . '&amp;image=' . $row['image'] . '">Approve</a></td></tr>';
   }
  }
  echo '</table>';

  mysqli_close($dbc);
?>


</body>
</html>


and an approveimage.php and removeimage.php pages. I'll provide just the approveimage.php page.

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Approve an image</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Approve an image</h2>

<?php
  require_once('appvars.php');
  require_once('connectvars.php');

  if (isset($_GET['id']) && isset($_GET['date']) && isset($_GET['name']) && isset($_GET['score'])) {
    // Grab the score data from the GET
    $id = $_GET['id'];
    $date = $_GET['date'];
    $name = $_GET['name'];
    $score = $_GET['score'];
    $screenshot = $_GET['image'];
  }
  else if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['score'])) {
    // Grab the score data from the POST
    $id = $_POST['id'];
    $name = $_POST['name'];
    $score = $_POST['score'];
  }
  else {
    echo '<p class="error">Sorry, no high score was specified for approval.</p>';
  }

  if (isset($_POST['submit'])) {
    if ($_POST['confirm'] == 'Yes') {
      // Connect to the database
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

      // Approve the score by setting the approved column in the database
      $query = "UPDATE photogallery SET approved = 1 WHERE id = $id";
      mysqli_query($dbc, $query);
      mysqli_close($dbc);

      // Confirm success with the user
      echo '<p>The image was successfully approved.';
    }
    else {
      echo '<p class="error">Sorry, there was a problem approving the image.</p>';
    }
  }
  else if (isset($id) && isset($name) && isset($date) && isset($desc)) {
    echo '<p>Are you sure you want to approve the following high score?</p>';
    echo '<p><strong>Name: </strong>' . $name . '<br /><strong>Date: </strong>' . $date .
      '<br /><strong>Description: </strong>' . $desc . '</p>';
    echo '<form method="post" action="approveimages.php">';
    echo '<img src="' . GW_UPLOADPATH . $screenshot . '" width="160" alt="image" /><br />';
    echo '<input type="radio" name="confirm" value="Yes" /> Yes ';
    echo '<input type="radio" name="confirm" value="No" checked="checked" /> No <br />';
    echo '<input type="submit" value="Submit" name="submit" />';
    echo '<input type="hidden" name="id" value="' . $id . '" />';
    echo '<input type="hidden" name="name" value="' . $name . '" />';
    echo '<input type="hidden" name="desc" value="' . $desc . '" />';
    echo '</form>';
  }

  echo '<p><a href="admin.php">&lt;&lt; Back to admin page</a></p>';
?>

</body>
</html>


The problem is no image is being approved i.e. setting the approved column to 1.

thanks

mark
mmarkym
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun May 22, 2011 1:57 am

Re: Uploading Images to a Page

Postby egami » Mon Jun 13, 2011 4:39 am

echo your sql query to the screen upon execution and see if the variable $id is being populated. I have a sneaky suspicion that it is not.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.