[英]PHP Self-referencing form
我正在使用Head First PHP和MYSQL教科书上PHP课程。 这本书写于2008年,所以我一直在想我所学的内容是否最新。
课本在php注释中显示了一种自引用html形式,如下所示:
<?php
require_once('appvars.php');
require_once('connectvars.php');
if (isset($_GET['id']) && isset($_GET['date']) && isset($_GET['name']) && isset($_GET['score']) && isset($_GET['screenshot'])) {
// Grab the score data from the Get
$id = $_GET['id'];
$date = $_GET['date'];
$name = $_GET['name'];
$score = $_GET['score'];
$screenshot = $_GET['screenshot'];
}
else if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['score'])) {
// Grab the score data from the POST
$id = $_POST['id'];
$name = $_POST['name'];
$score = $_POST['score'];
}
else {
echo '<p class="error">Sorry, no high score was specified for removal.</p>';
}
if (isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
// Delete the screen shot image file from the server
@unlink(GW_UPLOADPATH . $screenshot);
//Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Delete the score data from the database
$query = "DELETE FROM guitarwars WHERE id = $id LIMIT 1";
$data = mysqli_query($dbc, $query)
or die('Unable to complete query');
mysqli_close($dbc);
// Confirm success with the user
echo '<p>The high score of ' . $score . ' for ' . $name . ' was succesfully removed.';
}
else {
echo '<p class="error">The high score was not removed.</p>';
}
}
else if (isset($id) && isset($name) && isset($date) &&
isset($score) && isset($screenshot)) {
echo '<p>Are you sure you want to delete the following high score?</p>';
echo '<p><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 ';
echo '<input type="submit" value="Submit" name="submit">';
echo '<input type="hidden" name="id" value="' . $id . '">';
echo '<input type="hidden" name="score" value="' . $score . '">';
echo '<input type="hidden" name="screenshot" value="' . $screenshot . '">';
echo '</form>';
}
echo '<p><a href="admin.php"><< Back to admin page</a></p>';
?>
我不断收到错误消息,说我的id变量未定义。 经过研究后,我删除了动作引号内的文本。 现在可以用了,但是我仍然想知道这本书为什么用这种方式教书。 它们都是有效的选择吗?
回显的变量不带引号,因此,如果将其与常规HTML代码混合使用,则可以将引号用于HTML代码,然后以相同类型的另一个引号结尾(在本例中为单引号)。
但是由于HTML代码必须包含引号(对于“ value”属性),因此您可以像使用混合单引号和双引号一样。
变量之前的单引号结束HTML的回显,点允许连接变量,变量后的点则执行相同的操作,然后再次为HTML代码提供单引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.