Please Help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluetonic
Forum Newbie
Posts: 3
Joined: Mon Jun 22, 2009 11:50 am

Please Help!

Post by bluetonic »

It seems that no matter what I do, all the index page will do is return the "unverified!" gif. What's frustrating is that I understand that the problem is that the code either doesn't find the file or thinks its filesize !> 0.

The files are definitely in the the images folder. If I delete the "unverified" image, it resorts to the alt text. So, I am at a loss b/c it can find the folder and the unverified image, but can't associate the individual images with the players.

Is there any chance that my problems come from a setting on the server? I have even printed out my code and the "final" code, placed both pages together and held them up to a light to see if I could find a missing comma, semi-colon, etc.

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>Guitar Wars - High Scores</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Guitar Wars - High Scores</h2>
<p>Welcome, Guitar Warrior, do you have what it takes to crack the high score list?
If so, just <a href="addscore.php">add your own score</a>.</p>
<hr />
 
<?php
//Define the upload path and mixum file size constants
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 guitarwars";
$data = mysqli_query($dbc, $query);
 
// Loop through the array of score data, formatting it as HTML
echo '<table>';
while ($row = mysqli_fetch_array($data)) {
// Display the score data
echo '<tr><td class="scoreinfo">';
echo '<span class="score">' . $row['score'] . '</span><br />';
echo '<strong>Name:</strong> ' . $row['name'] . '<br />';
echo '<strong>Date:</strong> ' . $row['date'] . '</td>';
if (is_file(GW_UPLOADPATH . $row['screenshot']) && filesize(GW_UPLOADPATH . $row['screenshot']) > 0) {
echo '<td><img src="' . GW_UPLOADPATH . $row['screenshot'] . '" alt="Score image" /></td></tr>';
}
else {
echo '<td><img src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified score" /></td></tr>';
}
}
echo '</table>';
 
mysqli_close($dbc);
?>
 
</body>
</html>
Last edited by Benjamin on Tue Jun 23, 2009 10:43 am, edited 1 time in total.
Reason: Changed code type from text to php.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Please Help!

Post by Eric! »

So your file is uploaded ok, and you can browse to it directly with your browser to display it using the path that you are posting?

Can you post the HTML that is output from your script?

My first guess is you need to reformat your path name to be an html link. I looks like your posting the img src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified score" with a file path and you need to insert the http://yourdomain/ before the filepath.
bluetonic
Forum Newbie
Posts: 3
Joined: Mon Jun 22, 2009 11:50 am

Re: Please Help!

Post by bluetonic »

Eric! wrote:So your file is uploaded ok, and you can browse to it directly with your browser to display it using the path that you are posting?

Can you post the HTML that is output from your script?

My first guess is you need to reformat your path name to be an html link. I looks like your posting the img src="' . GW_UPLOADPATH . 'unverified.gif' . '" alt="Unverified score" with a file path and you need to insert the http://yourdomain/ before the filepath.
I can access the image directly.

There is no html, but this the other page of the site. You can try it if you like.
Index page: http://www.postmodernatl.com/
Add score page: http://www.postmodernatl.com/addscore.php

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>Guitar Wars - Add Your High Score</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>
        <h2>Guitar Wars - Add Your High Score</h2>
 
<?php
    require_once('appvars.php');
    require_once('connectvars.php');
 
  if (isset($_POST['submit'])) {
    // Grab the score data from the POST
    $name = $_POST['name'];
    $score = $_POST['score'];
    $screenshot = $_FILES['screenshot']['name'];
    
    if (!empty($name) && !empty($score) && !empty($screenshot)) {
    // Move the file to the target upload folder
    $target = GW_UPLOADPATH . $screenshot;
          if (move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
            // Connect to the database
            $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
 
      // Write the data to the database
      $query = "INSERT INTO guitarwars VALUES (0, NOW(), '$name', '$score', 'screenshot')";
      mysqli_query($dbc, $query);
        
      
      // Confirm success with the user
      echo '<p>Thanks for adding your new high score!</p>';
      echo '<p><strong>Name:</strong> ' . $name . '<br />';
      echo '<strong>Score:</strong> ' . $score . '<br />';
      echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
      echo '<p><a href="index.php"><< Back to high scores</a></p>';
 
      // Clear the score data to clear the form
      $name = "";
      $score = "";
 
      mysqli_close($dbc);
    }
    else {
      echo '<p class="error">Please enter all of the information to add your high score.</p>';
    }
  }
}
?>
        <hr />
        <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="hidden" name="MAX_FILE_SIZE" value="32768" />
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /><br />
            <label for="score">Score:</label>
            <input type="text" id="score" name="score" value="<?php if (!empty($score)) echo $score; ?>" /><br />
            <label for="screenshot">Screen shot:</label>
            <input type="file" id="screenshot" name="screenshot" />
            <hr />
            <input type="submit" value="Add" name="submit" />
        </form>
    </body>
</html>
Last edited by Benjamin on Tue Jun 23, 2009 10:43 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Please Help!

Post by califdon »

bluetonic wrote:
Eric! wrote:There is no html, but this the other page of the site. You can try it if you like.
Index page: http://www.postmodernatl.com/
What Eric! is asking for is the source HTML that your browser is seeing. That's the easiest way to see what the problem is. Navigate to your page in a browser, use the View menu to View Source and copy that code. When you post it here, enclose all the code between

Code: Select all

 and [/code ] tags (without the spaces) to make it more easily read.
bluetonic
Forum Newbie
Posts: 3
Joined: Mon Jun 22, 2009 11:50 am

Re: Please Help!

Post by bluetonic »

Thank you all for the help. I figured out I had left off the $ when creating value to the "screenshot" column. So instead of $screenshot it read screenshot, giving each item uploaded the name screenshot.

The one $ tripped me up for about two days. As I am new to PHP, I am still learning what errors look like.
Post Reply