Hi All,
I've been working on this all day and I can't figure out why the data will not show up after the form has been submitted. If I change this:
if (!$_REQUEST['Submit']) {
html_form();
} elseif (!$_REQUEST['Submit'] == "view") {
select_cd();
} elseif (!$_REQUEST['Submit'] == "Edit") {
get_data();
} elseif (!$_REQUEST['Submit'] == "Update") {
update_cd();
}
to this:
if (!$_REQUEST['Submit']) {
html_form();
} else {
select_cd();
}
Then the form gets the values from the database and puts them on the page but then the link to edit or update don't work because they are not in the if statement.
<html>
<head>
<title>Edit pages</title>
</head>
<body>
<?php
if (!$_REQUEST['Submit']) {
html_form();
} elseif (!$_REQUEST['Submit'] == "view") {
select_cd();
} elseif (!$_REQUEST['Submit'] == "Edit") {
get_data();
} elseif (!$_REQUEST['Submit'] == "Update") {
update_cd();
}
function my_conn() {
/* set's the variables for MySQL connection */
$server = ""; // this is the server address and port
$username = ""; // change this to your username
$password = ""; // change this to your password
/* Connects to the MySQL server */
$link = @mysql_connect ($server, $username, $password) or die (mysql_error());
/* Defines the Active Database for the Connection */
if (!@mysql_select_db("workshop", $link)) {
echo "<p>There has been an error. This is the error message:</p>";
echo "<p><strong>" . mysql_error() . "</strong></p>";
echo "Please Contact Your Systems Administrator with the details";
}
return $link;
}
function html_form() {
$conn = my_conn();
/* Defines query */
$sql = "SELECT DISTINCT subheaderText FROM subheaderText;";
/* Passes query to database */
$result = mysql_query($sql, $conn);
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Outputs HTML form */
?>
<p>Please enter the search term for the database</p>
<form name="cds" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
Name of Artist: <select name="subheaderText">
<?
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo("<option value=\"" . $row["subheaderText"] . "\">" . $row["subheaderText"] . "</option>\n");
}
?>
</select>
<input type="submit" name="Submit" value="view" />
</form>
<?
/* Closes Connection to the MySQL server */
mysql_close ($conn);
}
function select_cd() {
?>
<?
$conn = my_conn();
/* Sets the SQL Query */
$sql = "SELECT * FROM subheaderText";
$sql .= " WHERE (subheaderText = '{$_POST['subheaderText']}')";
/* Passes a Query to the Active Database */
$result = mysql_query($sql, $conn);
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Starts the table and creates headings */
?>
<table>
<tr>
<td><strong>subheaderText</strong></td>
<td><strong>headerText</strong></td>
<td><strong>page </strong></td>
<td><strong>No of pages</strong></td>
<td></td>
</tr>
<?
/* Retrieves the rows from the query result set
and puts them into a HTML table row */
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo("<tr>\n<td>" . $row["subheaderText"] . "</td>");
echo("<td>" . $row["headerText"] . "</td>");
echo("<td>" . $row["page"] . "</td>");
echo("<td><a href=\"" . $_SERVER['PHP_SELF'] . "?subheaderTextid=" .$row['subheaderTextid'] . "&Submit=Edit\">Edit</a></td></tr>\n\n");
}
/* Closes the table */
?>
</table>
<?
/* Closes Connection to the MySQL server */
mysql_close ($conn);
html_form();
}
function get_data() {
/* Calls our connection function */
$conn = my_conn();
/* Defines query */
$sql = "SELECT * FROM subheaderText WHERE (subheaderTextid = " . $_REQUEST['subheaderTextid'] . ")";
/* Passes query to database */
$result = mysql_query($sql, $conn);
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* creates our row array with an if statement to report errors */
if ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {
/* prints out the artist and title */
print "<h4>$row[subheaderText] - $row[headerText]</h4>";
/* prints out our HTML form '\"' */
print "<form name=\"CDs\" method=\"post\" action=\"$_SERVER[PHP_SELF]\">";
/* Prints out hidden releaseID - we don't put this in the HTML form
so that the uer cannot edit the Key value in error */
print "<input type=\"text\" name=\"subheaderTextid\" value=\"$row[subheaderTextid]\">";
/* prints out our HTML table and fields 'escaping' any double quotes '\"' */
print "<table width=\"600\">
<tr>
<td width=\"150\"><strong>subheaderText</strong></td>
<td width=\"350\"><input type=\"hidden\" name=\"subheaderText\" value=\"$row[subheaderText]\"></td>
<td rowspan=\"5\" valign=\"top\"><input type=\"submit\" name=\"Submit\" value=\"Update\">
</td>
</tr>
<tr>
<td width=\"150\"><strong>headerText</strong></td>
<td width=\"350\"><input type=\"text\" name=\"headerText\" value=\"$row[headerText]\"></td>
</tr>
<tr>
<td width=\"150\"><strong>page</strong></td>
<td width=\"350\"><input type=\"text\" name=\"page\" value=\"$row[page]\"></td>
</tr>
</table>
</form>";
/* Second SQL query */
$sql_count = "SELECT * FROM subheaderText WHERE subheaderTextid =" . $_REQUEST['subheaderTextid'];
/* Passes count query to database */
$result_count = @mysql_query($sql_count, $conn);
if (!$result_count) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Counts the number of rows (therefore copies) */
$count = mysql_num_rows($result_count);
if ($count != 1) {
print "<p>There are $count copies of this header</p>";
} else {
print "<p>There is $count copy of this item</p>";
}
} else {
echo("There has been an error" . mysql_error());
}
/* closes connection */
mysql_close ($conn);
}
function update_cd() {
/* Calls our connection function */
$conn = my_conn();
/* Defines query */
$sql_update = "UPDATE subheaderText SET ";
$sql_update .= "subheaderText = '" . $_REQUEST['subheaderText'] . "', ";
$sql_update .= "headerText = '" . $_REQUEST['headerText'] . "', ";
$sql_update .= "page = '" . $_REQUEST['page'] . "', ";
$sql_update .= "WHERE (subheaderid = " . $_REQUEST['subheaderid'] . ")";
/* Passes query to database */
$result = mysql_query($sql_update, $conn);
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
/* Prints succes message */
print "<p> Successfully Updated</p>";
/* closes connection */
mysql_close ($conn);
/* Calls get_data() function */
get_data();
}
?>
</body>
</html>