Forum Moderators: coopster
line 108:<form method="post" action="edit.php">
entire code:
<?php
//The following PHP script allows you to edit the
//contents of your MySQL table.
//Connecting to the MySQL database
require('dbconnect.php');
//Should we show a single item or a list?
if($_POST['edit']) {
//Simplifying the variables.
$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$date = $_POST['date'];
//trim() strips white space from the beginning and end of a line.
$date = trim($date);
$content = $_POST['content'];
//Checks for empty fields or invalid date.
if((empty($title)) OR (empty($author)) OR (empty($date)) OR (empty($content))) {
echo "<center><strong>Please fill in all fields!</strong></center>
";
} else {
//explode() separates the date by the '/' character and outputs it to an array.
$explode_date = explode('/', $date);
//checkdate() returns FALSE if the date is invalid.
$check_date = checkdate($explode_date[0], $explode_date[1], $explode_date[2]);
if($check_date == false) {
echo "<center><strong>Invalid date entered!</strong></center>
";
} else {
//htmlspecialchars() converts special characters into HTML entities.
$title = htmlspecialchars($title);
$author = htmlspecialchars($author);
//The MySQL query which will update the content in the table.
$query = "UPDATE news SET title = '$title', author = '$author', date = '$date', content = '$content' WHERE ID = '$id'";
//Execute the query.
$result = mysql_query($query) or die(mysql_error());
echo "<center><strong>News item modified!</strong></center>";
}
}
} elseif($_GET['action'] == "edit") {
//Display a single result.
$id = $_GET['id'];
//The MySQL query. Select all from the table news where the ID equals the id sent in URL.
$query = "SELECT * FROM news WHERE ID='$id'";
//Executing the query.
$result = mysql_query($query) or die(mysql_error());
//Displaying the results of the query.
while ($row = mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.
extract($row);
<form method="post" action="edit.php"> (line 108)
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" value="echo'$title'; " maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" value="echo '$author'; " maxlength="250" /></td></tr>
<tr><td align="right">Date:</td><td><input type="text" name="date" value="echo '$date'; " maxlength="10" /></td></tr>
<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10">echo'$content'; </textarea></td></tr>
<tr><td> </td><td><input type="hidden" name="id" value=" echo '$ID'; " /><input type="submit" name="edit" value="Modify" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>
}
} else {
//Since we're not displaying a single result,
//we're going to display a list of results.
//The MySQl query. Selects all from the table news.
$query = "SELECT * FROM news ORDER BY ID DESC";
//Execute the query.
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.
extract($row);
echo '<table><tr><td><strong><a href="edit.php?action=edit&id=$ID">$title</a></strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>';
<tr><td><strong><a href="delete.php?id=$ID">DELETE</a></strong></td></tr>?>
echothis HTML block (as you have done in other parts of your code) - but you also need to tidy up the echo statements within the value attributes of this HTML.
The very last line of your code appears to have the same problem.
<form method="post" action="edit.php">
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" value="echo'$title'; " maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" value="echo '$author'; " maxlength="250" /></td></tr>
<tr><td align="right">Date:</td><td><input type="text" name="date" value="echo '$date'; " maxlength="10" /></td></tr>
<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10">echo'$content'; </textarea></td></tr>
<tr><td> </td><td><input type="hidden" name="id" value=" echo '$ID'; " /><input type="submit" name="edit" value="Modify" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>
is not properly written. Let's use heredoc syntax [us3.php.net] to correct it now:
echo <<<HTML
<form method="post" action="edit.php">
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" value="$title" maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" value="$author" maxlength="250" /></td></tr>
<tr><td align="right">Date:</td><td><input type="text" name="date" value="$date" maxlength="10" /></td></tr>
<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10">$content</textarea></td></tr>
<tr><td> </td><td><input type="hidden" name="id" value="$ID" /><input type="submit" name="edit" value="Modify" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>
HTML;
Take careful note of the changes that I made between the two pieces of code. Also, as was already said, the last line has a similar issue as it's not being echoed to the screen properly and the PHP interpreter thinks it's PHP code.
So this line here:
<tr><td><strong><a href="delete.php?id=$ID">DELETE</a></strong></td></tr>?>
can be made with just a basic echo:
echo '<tr><td><strong><a href="delete.php?id=' . $ID .'">DELETE</a></strong></td></tr>';
?>
line 167 is the very last line of code which is </html>
the entire code is :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
#Layer1 {
position:absolute;
left:413px;
top:63px;
width:200px;
height:212px;
z-index:1;
}
.style1 {color: #FFFFFF}
-->
</style>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
</head>
<body id="page1">
<table class="table">
<tr>
<td class="site_center">
<table class="site_center1">
<tr>
<td id="header">
<div class="indent">
<a href="#"><img alt="" src="images/text_banner.jpg" class="txt_banner" width="93" height="39" /></a><br />
<a href="index.html"><img alt="" src="images/logo.jpg" width="334" height="75" /></a> </div></td>
</tr>
<tr>
<td id="content">
<div class="block">
<div class="top">
<div class="bottom">
<div class="inside">
<p align="center">Wl<span class="style1">Welcome to Loft Night Club.</span></p>
<table width="100%" height="207" border="0">
<tr>
<td width="100%">
<?php
//The following PHP script allows you to edit the
//contents of your MySQL table.
//Connecting to the MySQL database
require('dbconnect.php');
//Should we show a single item or a list?
if($_POST['edit']) {
//Simplifying the variables.
$id = $_POST['id'];
$title = $_POST['title'];
$author = $_POST['author'];
$date = $_POST['date'];
//trim() strips white space from the beginning and end of a line.
$date = trim($date);
$content = $_POST['content'];
//Checks for empty fields or invalid date.
if((empty($title)) OR (empty($author)) OR (empty($date)) OR (empty($content))) {
echo "<center><strong>Please fill in all fields!</strong></center>
";
} else {
//explode() separates the date by the '/' character and outputs it to an array.
$explode_date = explode('/', $date);
//checkdate() returns FALSE if the date is invalid.
$check_date = checkdate($explode_date[0], $explode_date[1], $explode_date[2]);
if($check_date == false) {
echo "<center><strong>Invalid date entered!</strong></center>
";
} else {
//htmlspecialchars() converts special characters into HTML entities.
$title = htmlspecialchars($title);
$author = htmlspecialchars($author);
//The MySQL query which will update the content in the table.
$query = "UPDATE news SET title = '$title', author = '$author', date = '$date', content = '$content' WHERE ID = '$id'";
//Execute the query.
$result = mysql_query($query) or die(mysql_error());
echo "<center><strong>News item modified!</strong></center>";
}
}
} elseif($_GET['action'] == "edit") {
//Display a single result.
$id = $_GET['id'];
//The MySQL query. Select all from the table news where the ID equals the id sent in URL.
$query = "SELECT * FROM news WHERE ID='$id'";
//Executing the query.
$result = mysql_query($query) or die(mysql_error());
//Displaying the results of the query.
while ($row = mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.
echo <<<HTML
<form method="post" action="edit.php">
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" value="$title" maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" value="$author" maxlength="250" /></td></tr>
<tr><td align="right">Date:</td><td><input type="text" name="date" value="$date" maxlength="10" /></td></tr>
<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10">$content</textarea></td></tr>
<tr><td> </td><td><input type="hidden" name="id" value="$ID" /><input type="submit" name="edit" value="Modify" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>
HTML;
}
} else {
//Since we're not displaying a single result,
//we're going to display a list of results.
//The MySQl query. Selects all from the table news.
$query = "SELECT * FROM news ORDER BY ID DESC";
//Execute the query.
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
//extract() takes an associative array and treats the keys as variable names and values as variable values.
extract($row);
echo '<table><tr><td><strong><a href="edit.php?action=edit&id='.$ID.'">$title</a></strong></td></tr>
<tr><td><small>Written by $author on $date</small></td></tr>';
echo '<tr><td><strong><a href="delete.php?id=' . $ID .'">DELETE</a></strong></td></tr>';
?>
</td>
</tr>
</table>
</div>
</div>
</div>
</div></td>
</tr>
<tr>
<td id="footer">
<div class="indent">
<div class="row_1">
<ul class="footer_menu">
<li><a class="current" href="index-1.html">home page</a></li>
<li><a href="index-2.html">about us</a></li>
<li><a href="index-3.html">our menu</a></li>
<li><a href="index-4.html">concerts</a></li>
<li><a href="index-5.html">our gallery</a></li>
<li><a href="index-6.html">our news</a></li>
<li><a href="index-7.html">download</a></li>
<li><a href="index-8.html">locations</a></li>
</ul>
</div>
Copyright 2007 © Night Club. All rights reserved. <span><a href="index-9.html">Privacy Policy</a></span> ¦ <span><a href="#">Terms Of Use</a></span></div></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>