I'm taking a PHP course, using the Head First PHP and MYSQL textbook. The book was written back in 2008 so I am constantly wondering if what I'm learning is up to date.
The text book shows a self-referencing html form in php comments like this:
<?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>';
?>
I kept getting an error message saying that my id variable is undefined. After doing some research, I removed the text inside of the action quotes. Now it works, but I'm still wondering why the book teaches this way. Are they both valid options?
A variable is echoed without quotes, so if you mix it with regular HTML code, you use the quotes for the HTML code, then end that with another quote of the same type - in your case a single quote.
But since the HTML code has to contain quotes (for the "value" attribute), you can mix single and double quotes as you did it.
The single quote before the variable ends the HTML echoing, the dot allows to connect a variable, after the variable a dot does the same, and then again single quotes for the HTML code.