Mekanic
Mekanic

Reputation: 199

I need to know why my code is telling me the $id variable is undefined

This code is suppose to grab data from a database, display it and then pass the reference data to removescore.php.

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

//Connect to database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
//Query
//Remember: Query suppose to have in the table name not the database name
$query = "SELECT * FROM table ORDER BY score DESC, date ASC";
//Function
$data = mysqli_query($dbc, $query);

//Loop through the array of the score and format it as html
echo '<table>';
while ($row = mysqli_fetch_array($data)){
  echo '<tr class="scorerow"><td><strong>' . $row['name'] . '</strong></td>';
  echo '<td>' . $row['id'] . '</td>'; 
  echo '<td>' . $row['date'] . '</td>';
  echo '<td>' . $row['score'] . '</td>';
  echo '<td><a href="removescore.php?id=' . $row['id'] . '&amp;date=' .
 $row['date'] . '&amp;name= ' . $row['name'] . '&amp;score= ' . $row['score'] .
   '&amp;screenshot= ' . $row['screenshot'] . '">Remove</a></td></tr>';
}
echo '</table>';
mysqli_close($dbc);

?>

This program below is the removescore.php program.

 <?php
 #THIS APP REMOVES AND DISPLAY A score FROM 
 #THE GUITAR WARS APPLICATION
 require_once('connectvars.php');
 require_once('appvars.php');

if (isset($_GET['id']) && isset($_GET['date']) && 
    isset($_GET['name']) && isset($_GET['score']) &&                    
    isset($_GET['screenshot'])) {

    $id = $_GET['id'];
    $date = $_GET['date'];
    $name = $_GET['name'];
    $score = $_GET['score'];
    $screenshot = $_GET['screenshot'];

} else if (isset($_POST['id']) && isset($_POST['date']) &&
           isset($_POST['name']) && isset($_POST['score']) && 
           isset($_POST['screenshot'])) {

    $id = $_POST['id'];
    $date = $_POST['date'];
    $name = $_POST['name'];
    $score = $_POST['score'];
    $screenshot = $_POST['screenshot'];
} else {
    echo'<p>No highscore was specified for removal.</p>';
}
if (isset($_POST['submit'])) {
    if ($_POST['confirm'] == 'Yes') {
        @unlink(GW_UPLOADPATH . $screenshot);
        $dbc =  mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        $query = "DELETE FROM table WHERE id = $id LIMIT 1";
        mysqli_query($dbc, $query);
        mysqli_close($dbc);
        echo'<p>Highscore was removed</p>';
    } else {
        echo'<p>No highschore was specified for removal.</p>';
    }
} else if (isset($id) && isset($name) && 
           isset($date) && isset($score) && 
           isset($screenshot)) {
    echo '<p>Are you sure you want to delete this highscore?</p>';
    echo '<p>
             <strong>Id:</strong> '. $id .' <br/> 
             <strong>Name:</strong> ' . $name . ' <br/> 
             <strong>Date:</strong> '. $date  .' <br/>
             <strong>Score:</strong> '. $score . '  
         </p>';
    echo '<form method="post" action="removescore.php">';
    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="score" value="' . $score . '" />';
    echo '<input type="hidden" name="screenshot" 
                 value="' . $screenshot . '" />';
    echo '</form';
}
echo '<p><a href="admin.php">&lt;&lt; Back to main page</a></p>';
?>

So this is it. I've been trying to get this problem fixed for about a month. I'm a beginner. All the help is greatly appreciated.

Upvotes: 1

Views: 122

Answers (4)

birraa
birraa

Reputation: 440

From your code, I can see that some of the key-value pairs appended to the hyperlink contain unnecessary space. For example,

name= ' . $row['name'] 

has extra space which I think you didn't intend to put there. Sure, that is what is causing the problem of course. Besides, why do you have to use &amp in the hyperlink? Why don't you just use & directly?

Upvotes: 0

Kamall A Joshi
Kamall A Joshi

Reputation: 1298

You are not passing date in your hidden fields so if you try to print $_POST variable, it wont be available and your condition fails as it expects

if (isset($_POST['id']) && isset($_POST['date']) && isset($_POST['name']) && isset($_POST['score']) && isset($_POST['screenshot'])){

Just add the following line in the code below id hidden.

 echo'<input type="hidden" name="id" value="' . $id . '" />';
 echo'<input type="hidden" name="date" value="' . $date . '" />';

Upvotes: 1

l00k
l00k

Reputation: 1545

Try this code to ensure what condition is fulfilled.

if (isset($_GET['id']) && isset($_GET['date']) && isset($_GET['name']) && isset($_GET['score'])

 && isset($_GET['screenshot'])){

$id = $_GET['id'];
$date = $_GET['date'];
$name = $_GET['name'];
$score = $_GET['score'];
$screenshot = $_GET['screenshot'];

echo '1 block';
die;

}
else if (isset($_POST['id']) && isset($_POST['date']) && isset($_POST['name']) && isset($_POST['score']) && isset($_POST['screenshot    '])){

$id = $_POST['id'];
$date = $_POST['date'];
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot = $_POST['screenshot'];

echo '2 block';
die;
}

Upvotes: 0

Fuseteam
Fuseteam

Reputation: 396

in the removescore.php program in the else if (isset($_POST['id']) &&.......) you schould propably remove the tab from isset($_POST['screenshot '] and under if (isset($_POST['submit'])) you should propably replace $query = "DELETE FROM table WHERE id = $id LIMIT 1"; with $query = "DELETE FROM table WHERE id = ".$id." LIMIT 1"; note the ".$id." difference

Upvotes: 0

Related Questions