我需要知道为什么我的代码告诉我$ id变量是未定义的

时间:2015-09-19 07:39:27

标签: php mysql

此代码假设从数据库中获取数据,显示它然后将引用数据传递给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);

?>

以下程序是removescore.php程序。

 <?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>';
?>

    所以就是这样。我一直试图将这个问题修复一个月左右。我是初学者。非常感谢所有的帮助。

4 个答案:

答案 0 :(得分:1)

您没有在隐藏字段中传递日期,因此如果您尝试打印$ _POST变量,它将无法使用,并且您的条件会因预期而失败

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

只需在id隐藏的代码中添加以下行。

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

答案 1 :(得分:0)

else if (isset($_POST['id']) &&.......)中的removescore.php程序中,你可以从isset($_POST['screenshot ']中删除标签 并在if (isset($_POST['submit']))下 你应该适当地替换 $query = "DELETE FROM table WHERE id = $id LIMIT 1";$query = "DELETE FROM table WHERE id = ".$id." LIMIT 1"; 请注意".$id."差异

答案 2 :(得分:0)

尝试使用此代码以确保满足的条件。

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;
}

答案 3 :(得分:0)

从您的代码中,我可以看到附加到超链接的一些键值对包含不必要的空间。例如,

name= ' . $row['name'] 

有额外的空间,我认为你不打算放在那里。当然,这正是造成问题的原因。 此外,为什么你必须在超链接中使用&amp; amp?你为什么不用&amp;直接?