PHP Issue Undefined Index

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
gilal
Forum Newbie
Posts: 4
Joined: Fri Jan 16, 2009 11:50 pm

PHP Issue Undefined Index

Post by gilal »

Hi guys.

I am following Oriely's Head first php and mysql and i am stuck with an error i am getting which i tried to figure out for a few days with no success.

I am getting: Undefined Index: screenshot line 21

Here is the script:

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
//Define the upload path and maximum file size constants
define('GW_UPLOADPATH', 'images/');
 
  if (isset($_POST['submit'])) {
  echo 'Something Simple';
      // Grab the score data from the POST
    $name = $_POST['name'];
    $score = $_POST['score'];
    $screenshot = $_FILES['screenshot'];
 
    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('localhost', 'root', 'towerair', 'chapter5');
 
      // 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 . '</p>';
      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 method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <form enctype="multi-part/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; ?>" />
    <input type="file" id="screenshot" name="screenshot" />
    <hr />
    <input type="submit" value="Add" name="submit" />
  </form>
</body>
</html>
 
If anyone can help me Id be very appreciative.

Thanks
Gil
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: PHP Issue Undefined Index

Post by Burrito »

call is_uploaded_file() before you set your variable.
gilal
Forum Newbie
Posts: 4
Joined: Fri Jan 16, 2009 11:50 pm

Re: PHP Issue Undefined Index

Post by gilal »

Thanks for the quick reply.

Since I am following a text-book which does not specify that function, can you please explain more about where to add the is_uploaded() function?

Thanks
Gil
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: PHP Issue Undefined Index

Post by Burrito »

Code: Select all

 
if(is_uploaded_file($_FILES['screenshot']['tmp_name']))
  $screenshot &= $_FILES[...]
 
Post Reply