Cleaning up code with IF ?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Cleaning up code with IF ?

Post by sergei »

Deletenews.php file

Code: Select all

<?php
				$relative_script_path = '.'; 
				include("$relative_script_path/config/config.php"); 
				$connection = mysql_connect($DBHOST,$DBUSER) 
					or die("Connection unsuccessful: " . mysql_error());    								
				
				mysql_select_db($DBASE) 
				or die("Connection to database unsuccessful: " . mysql_error()); 
				
				$sql = "SELECT * FROM news_table"; 

				$result = mysql_query($sql,$connection); 

				echo "<TABLE BORDER=2 align=center width=472>";
				echo"<TR><TD><B>Title</B></TD><TD><B>Author</B></TD><TD><B>Short Story</B></TD></TR>";
				
				while ($row = mysql_fetch_array($result))
				{
					echo "<TR><TD>".$row["title"]."<br />".$row["author"]."</TD>";
					echo "<TD><a href="viewnews.php?id=".$row[id]."">View</a>&nbsp; ";
					echo "<a href="delete.php?id=".$row[id]."">Delete</a></TD>";
					echo "<TD>".$row["shortstory"]."</TD></TR>";
				}
				echo "</TABLE>";			

				mysql_close($connection);

?>
Delete.php file

Code: Select all

<?php

				$relative_script_path = '.'; 
				include("$relative_script_path/config/config.php"); 
				$connection = mysql_connect($DBHOST,$DBUSER) 
					or die("Connection unsuccessful: " . mysql_error());    								


				mysql_select_db($DBASE) 
				or die("Connection to database unsuccessful: " . mysql_error()); 
				
				$sql = "DELETE FROM news_table WHERE id=$id"; 
				mysql_query($sql,$connection) or die("Message could not be deleted: " . mysql_error()); 
					echo("Your selected row was successfully deleted. Thank you.<br /><br />");
							
				mysql_close($connection);

?>
Can someone help me combine these two file into one so that when I delete the record it returns to the initial view / delete section.

Thanks.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

I think you mean shownews.php rather than delete.php for the first one, since I don't see any deleting anywhere but outputting everywhere.

Maybe something like:

Code: Select all

<?php

$action = $_GET['action'];

if(isSet($action) {

if($action=="delete") {
// delete the news
}

else {
// show the news
}

}

else {
die("Action not set");
}

?>
Is that what you meant?

-Nay
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Post by sergei »

Code: Select all

<?php

$relative_script_path = '.'; 
include("$relative_script_path/config/config.php"); 
mysql_connect($DBHOST,$DBUSER) 
	or die("Connection unsuccessful: " . mysql_error());    								

mysql_select_db($DBASE) 
or die("Connection to database unsuccessful: " . mysql_error()); 

$action = $_GET['action']; 

if(isSet($action) { 

if($action=="delete")  { // delete the news 
	$sql = "DELETE FROM news_table WHERE id=$id"; 
	mysql_query($sql) or die("Message could not be deleted: " . mysql_error()); 
		echo("Your selected row was successfully deleted. Thank you.<br /><br />");

	$query = "SELECT * FROM news_table"; 

	$result = mysql_query($query); 

	echo "<TABLE BORDER=2 align=center width=472>";
	echo"<TR><TD><B>Title</B></TD><TD><B>Author</B></TD><TD><B>Short Story</B></TD></TR>";
	while ($row = mysql_fetch_array($result))
	{
		echo "<TR><TD>".$row["title"]."<br />".$row["author"]."</TD>";
		echo "<TD><a href="viewnews.php?id=".$row[id]."">View</a>&nbsp; ";
		echo "<a href="delete.php?id=".$row[id]."">Delete</a></TD>";
		echo "<TD>".$row["shortstory"]."</TD></TR>";
	}
	echo "</TABLE>";
} 

else { // show the news 

	$result = mysql_query("SELECT * FROM news_table LIMIT 5");  //$start,$limit");  

	echo "<TABLE BORDER=2 align=center width=472>";
	echo"<TR><TD><B>Title</B></TD><TD><B>Author</B></TD><TD><B>Short Story</B></TD></TR>";

	while ($row = mysql_fetch_array($result))
	{
		echo "<TR><TD>".$row["title"]."<br />".$row["author"]."</TD>";
		echo "<TD><a href="viewnews.php?id=".$row[id]."">View</a>&nbsp; ";
		echo "<a href="delete.php?id=".$row[id]."">Delete</a></TD>";
		echo "<TD>".$row["shortstory"]."</TD></TR>";
	}
	echo "</TABLE><br />";			

}



?>
Like this it gives me the following error: Parse error: parse error, unexpected '{' in C:\Apache2\htdocs\metering\deletenews.php on line 343

line 343 equals

Code: Select all

if($action=="delete")  {
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

The problem is here:

Code: Select all

if(isSet($action) {
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Oh yeah, of course. Sorry dude, and thanks Jason. It should be:

Code: Select all

if(isSet($action)) {
-Nay
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Post by sergei »

Thanks for the change. I've done and it doesn't give me the error again.

Now nothing displays. Is this because the if statement is resolving to TRUE ?

How can I have the else statement display first and when you click to delete a record, the if statement displays ?

Thanks
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Change

if($action=="delete")

to

if($action!="delete")

Although I can't see how this would be your problem. And obviously switch your code blocks around.

Should

$row[id]

be

$row["id"]

?

I can't see any errors in the rest of your code.
User avatar
sergei
Forum Commoner
Posts: 33
Joined: Mon Oct 06, 2003 4:17 am

Post by sergei »

Works with this code.

Code: Select all

if(!isSet($action)) { 
								
								if($action!="delete")  {
Thanks for the help.
Post Reply