PHP MySQL news sys prob again

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks for the help before the '".$_GET['id']."'"; certainly fixed all the problems regarding the display of  stories and I have ammended my othe files similarly but I am having I think syntax problems with the add and edit parts. I'm sure its just a case of using the GET part again but am having some difficulty.

Thanks in Advance.

Code below

This is the correction made so far which displays stories, deletes and displays stories in the edit dialog
//$query = "SELECT slug, content, contact, timestamp FROM news WHERE id = '$id'";
$query = "SELECT slug, content, contact, timestamp FROM news WHERE id = '".$_GET['id']."'";


This is the add part:
// add.php - add a new press release
?>
<html>
<head>
<basefont face="Verdana">
</head>

<body>

<!-- standard page header begins -->
<p>&nbsp;<p>

<table width="100%" cellspacing="0" cellpadding="5">
<tr>
	<td></td>
</tr>
<tr>
	<td bgcolor="Navy"><font size="-1" color="White"><b>Megalomaniacs Inc : Administration : Press Releases : Add</b></font></td>
</tr>
</table>
<!-- standard page header ends -->

<p>

<?
// form not yet submitted
// display initial form
if (!$submit)
{
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $PHP_SELF; ?>" method="POST">
<tr>
	<td valign="top"><b><font size="-1">Slug</font></b></td>
	<td><input size="50" maxlength="250" type="text" name="slug"></td>
</tr>
<tr>
	<td valign="top"><b><font size="-1">Content</font></b></td>
	<td><textarea name="content" cols="40" rows="10"></textarea></td>
</tr>
<tr>
	<td valign="top"><font size="-1">Contact person</font></td>
	<td><input size="50" maxlength="250" type="text" name="contact"></td>
</tr>
<tr>
	<td colspan=2><input type="Submit" name="submit" value="Add"></td>
</tr>
</form>
</table>
<?
}
else
{
	// includes
	include("../conf.php");
	include("../functions.php");

	// set up error list array
	$errorList = array();
	$count = 0;
	
	// validate text input fields
	if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }
	
	if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }
	
	// set default value for contact person
	if (!$contact) { $contact = $def_contact; }
	
	// check for errors
	// if none found...
	if (sizeof($errorList) == 0)
	{
		// open database connection
		$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

		// select database
		mysql_select_db($db) or die ("Unable to select database!");

		// generate and execute query
		$query = "INSERT INTO news(slug, content, contact, timestamp) VALUES ('$slug', '$content', '$contact', NOW())";
		$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

		// print result
		echo "<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>";

		// close database connection
		mysql_close($connection);
	}
	else
	{
		// errors found
		// print as list
		echo "<font size=-1>The following errors were encountered: <br>";
		echo "<ul>";
		for ($x=0; $x<sizeof($errorList); $x++)
		{
			echo "<li>$errorList[$x]";
		}
		echo "</ul></font>";
	}
}
?>




</body>
</html>

This is the Edit part where the Update statement doesn't work although it retrieves the record in the editable formats.

// edit.php - edit a press release
?>
<html>
<head>
<basefont face="Verdana">
</head>

<body>

<!-- standard page header begins -->
<p>&nbsp;<p>

<table width="100%" cellspacing="0" cellpadding="5">
<tr>
	<td></td>
</tr>
<tr>
	<td bgcolor="Navy"><font size="-1" color="White"><b>Megalomaniacs Inc : Administration : Press Releases : Edit</b></font></td>
</tr>
</table>
<!-- standard page header ends -->

<p>

<?
// includes
include("../conf.php");
include("../functions.php");

// form not yet submitted
// display initial form with values pre-filled
if (!$submit)
{
	// open database connection
	$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

	// select database
	mysql_select_db($db) or die ("Unable to select database!");

	// generate and execute query
	//$query = "SELECT slug, content, contact FROM news WHERE id = '$id'";
	$query = "SELECT slug, content, contact, timestamp FROM news WHERE id = '".$_GET['id']."'";
	$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
	
	// if a result is returned
	if (mysql_num_rows($result) > 0)
	{
		// turn it into an object
		$row = mysql_fetch_object($result);

		// print form with values pre-filled
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $PHP_SELF; ?>" method="POST">
<input type="hidden" name="id"  value="<? echo $id; ?>">
<tr>
	<td valign="top"><b><font size="-1">Slug</font></b></td>
	<td><input size="50" maxlength="250" type="text" name="slug" value="<? echo $row->slug; ?>"></td>
</tr>
<tr>
	<td valign="top"><b><font size="-1">Content</font></b></td>
	<td><textarea name="content" cols="40" rows="10"><? echo $row->content; ?></textarea></td>
</tr>
<tr>
	<td valign="top"><font size="-1">Contact person</font></td>
	<td><input size="50" maxlength="250" type="text" name="contact"  value="<? echo $row->contact; ?>"></td>
</tr>
<tr>
	<td colspan=2><input type="Submit" name="submit" value="Update"></td>
</tr>
</form>
</table>
<?
	}
	// no result returned
	// print graceful error message
	else
	{
		echo "<font size=-1>That press release could not be located in our database.</font>";
	}
}
// form submitted
// start processing it
else
{
	// set up error list array
	$errorList = array();
	$count = 0;
	
	// validate text input fields
	if (!$slug) { $errorList[$count] = "Invalid entry: Slug"; $count++; }
	
	if (!$content) { $errorList[$count] = "Invalid entry: Content"; $count++; }
	
	// set default value for contact person
	if (!$contact) { $contact = $def_contact; }
	
	// check for errors
	// if none found...
	if (sizeof($errorList) == 0)
	{
		// open database connection
		$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

		// select database
		mysql_select_db($db) or die ("Unable to select database!");

		// generate and execute query
		//$query = "SELECT slug, content, contact, timestamp FROM news WHERE id = '".$_GET['id']."'";
		$query = "UPDATE news SET slug = '$slug', content = '$content', contact = '$contact', timestamp = NOW() WHERE id = '$id'";
		$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

		// print result
		echo "<font size=-1>Update successful. <a href=list.php>Go back to the main menu</a>.</font>";

		// close database connection
		mysql_close($connection);
	}
	else
	{
		// errors occurred
		// print as list
		echo "<font size=-1>The following errors were encountered: <br>";
		echo "<ul>";
		for ($x=0; $x<sizeof($errorList); $x++)
		{
			echo "<li>$errorList[$x]";
		}
		echo "</ul></font>";
	}
}
?>
</body>
</html>



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux