gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:59:08 24 4
gpt4 key购买 nike

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

所以就是这样。我已经尝试解决这个问题大约一个月了。我是初学者。非常感谢所有的帮助。

最佳答案

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

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

只需在 id hide 下面的代码中添加以下行即可。

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

关于php - 我需要知道为什么我的代码告诉我 $id 变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32665651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com