Hide FORM to show results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Hide FORM to show results

Post by paquin1 »

Ok, here is my problem. I'm a newbie, and I'm learning as I try to set up an intranet in my company.

This is were I started for the code I have http://dev.mysql.com/tech-resources/art ... ws/24.html

I finally got it to work, without giving me any errors as a change some of the code because of bad syntax (since I'm a newbie it might still be bad) but anyway, is giving me no errors.

When I view my page it shows all the code to view mySQL info so I see the list of news, but when I click the Add NEWS, it refreshes and still shows the list of news but it does not show the form.
I read somewhere about SESSIONS, does this code should it be in sessions? Or I'm missing something? Anyway thanks in advanced to everyone that takes a look at this. Any other comments on coding are appreciated.

Code: Select all

<?php
// If the user wants to add a news 
		  if (isset($addnews)) {
?>
<p>Add news to db</p>
<p>Note: News will have the date and time of post. </p>
      
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<div align="left">
	News Title : 
	<input name="news_titlev" type="text">
	<br>
	News:<br>
	<textarea name="news_txtv" cols="40" rows="3"></textarea>
	<br>
	<input name="idv" type="hidden"> 
	<input name="news_datev" type="hidden">
	
	<br>
	<input type="submit" name="action" value="Submit News">           
 	</div>
</form>           
<?PHP  
	} else {
$dbcnx = @mysql_connect("localhost", "****", "****");
if (!$dbcnx) { 
	echo( "<P>Unable to connect to the database server at this time.</P>" );  
	exit();
}
mysql_select_db("intranet", $dbcnx); 
if (! @mysql_select_db("intranet") ) { 
	echo( "<P>Unable to locate the news database at this time.</P>" );  
	exit();
}   
// If a news has been submitted, add it to the database.   
if (isset($_POST['action']) && $_POST['action'] == 'submit') {
	$sql = "INSERT INTO hhmnews SET news_title='$news_titlev',  news_txt='$news_txtv', news_date=CURDATE()";
	if (mysql_query($sql)) {
	echo ("<P>Your news have been added.</P>");
	} else {
	echo("<P>Error adding submitted news.</P>");
		}
	}
	echo ("<P> here are all the news in our database:</P>");
	// Request the text of all the news  
	$result = mysql_query("SELECT * FROM hhmnews", $dbcnx);
	   if (!$result) {
	         echo("<P>Error performing query</P>");
			      exit();
				  }
		if ($rows = mysql_fetch_array($result)) {
	do {
	printf ("<i>%s</i><br>", $rows["news_date"]);
	printf ("<b>%s</b><br>", $rows["news_title"]);
	printf ("%s<p>", $rows["news_txt"]);
	} while ($rows = mysql_fetch_array ($result));
//if no news found
} else {
	echo "<p class='text1'>No news where found at this time</p>";
	}
	 // When clicked, this link will load this page
	 // with the news submission form displayed.
	     echo '<A HREF="'. $_SERVER['PHP_SELF'] . '?addnews=1">Add NEWS</A></P>';
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try using $_GET['addnews'] instead of $addnews


$addnews would work ONLY if register_globals were on, which they shouldn't be.
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

Thanks it worked great, and have made some changes in the code but now I have a different problem

Once I fill the form and submit, first it does not show me the message I have there that says, Your news have been added, and jumps to showing the database info, but does not add the new info I added with the form. When I take this code of INSERT INTO and added to another PHP page without this entire IF statements, it adds the data just fine. So I'm not sure if my IF statements are correct, they look logical to me but maybe they are not.

here is the new code:

Code: Select all

<?php
// If the user wants to add a news 
		  if (isset($_GET['addnews'])) {
?>
<p class='text1'>Add news to db</p>
<p class='text1'>Note: News will have the date and time of post. </p>
      
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <div align="left">    
    <p class='text1'>News Title:
        <input name="news_titlev" type="text" id="news_titlev" size="40">
        <br>
        News:<br>
        <textarea name="news_txtv" cols="40" rows="5" id="news_txtv"></textarea>
        <br>
        <input name="news_idv" type="hidden" id="news_idv">
        <input name="news_datev" type="hidden" id="news_datev">
        <br>
        <input type="submit" name="action" value="Submit">
        </p>
    </div>
</form>
<?PHP  
	} else {
$dbcnx = @mysql_connect("localhost", "****", "****");
if (!$dbcnx) { 
	echo( "<P>Unable to connect to the database server at this time.</P>" );  
	exit();
}
mysql_select_db("intranet", $dbcnx); 
if (! @mysql_select_db("intranet") ) { 
	echo( "<P>Unable to locate the news database at this time.</P>" );  
	exit();
}   
// If news has been submitted, add it to the database.   
if (isset($_POST['action']) && $_POST['action'] == 'submit')  {
	$query = "INSERT INTO hhmnews SET news_id='$_POST[news_idv].', news_txt='$_POST[news_txtv]', news_date=NOW(), news_title='$_POST[news_titlev]'";
	if ($result = mysql_query($query)) {
	echo ("<P>Your news have been added.</P>");
	exit();
	} else {
	echo("<P>Error adding submitted news.</P>");
	exit();
		}
	}
	echo ("<P>Here are all the news in our database:</P>");
	// Request the text of all the news  
	$result = mysql_query("SELECT * FROM hhmnews", $dbcnx);
	   if (!$result) {
	         echo("<P>Error performing query</P>");
			      exit();
				  }
		if ($rows = mysql_fetch_array($result)) {
	do {
	printf ("<i>%s</i><br>", $rows["news_date"]);
	printf ("<b>%s</b><br>", $rows["news_title"]);
	printf ("%s<p>", $rows["news_txt"]);
	} while ($rows = mysql_fetch_array ($result));
//if no news found
} else {
	echo "<p class='text1'>No news where found at this time</p>";
	}
	 // When clicked, this link will load this page
	 // with the news submission form displayed.
	     echo '<A HREF="'. $_SERVER['PHP_SELF'] . '?addnews=1">Add NEWS</A></P>';
}
?>
Last edited by paquin1 on Fri Aug 06, 2004 4:59 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

$query = "INSERT INTO hhmnews SET news_id='$_POST[news_idv].', news_txt='$_POST[news_txtv]', news_date=NOW(), news_title='$_POST[news_titlev]'";

why do you have a peroid after news_id='$_POST[news_idv](peroid)

?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Damn tim you must have some powerful vision there lol. Well done!
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

I have no idea, but ok is gone.
I mean the period, but the form still does not work.
Thanks
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Actually, the query should be like:

Code: Select all

$query = "INSERT INTO hhmnews SET news_id='".$_POST&#1111;news_idv]."', news_txt='".$_POST&#1111;news_txtv]."', news_date=NOW(), news_title='".$_POST&#1111;news_titlev]."'";
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

if ($result = mysql_query($query)) {
echo ("<P>Your news have been added.</P>");
exit();
}

this is a uncorrect if statement. forget the if statement and put the query after the insert command. use

or die (mysql_error()); to indicate there was an error, if any.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

It should just be like:

Code: Select all

$query = "INSERT INTO hhmnews SET news_id='".$_POST[news_idv]."', news_txt='".$_POST[news_txtv]."', news_date=NOW(), news_title='".$_POST[news_titlev]."'";
$result = mysql_query($query) or die(mysql_error());

print("News Added!");
If an error was to occur it would be handled by mysql_error() so no need to use mysql_num_rows or anything like that.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

but make sure to quote your named array indices! :D
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Also, for areas in your code such as if (! @mysql_select_db("intranet") ) , just use,

Code: Select all

mysql_select_db("intranet") or die(mysql_error());
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

Ok this is the code I have now, I took off all those IF statements that.... well, I don't need them... :?

No error on page, but still it doesn’t add the data to my db, I'm not sure I added or took off all that you have ask to take out but let me know...

Code: Select all

<?php
// If the user wants to add a news 
        if (isset($_GET['addnews'])) { 
?> 
<p class='text1'>Add news to db</p> 
<p class='text1'>Note: News will have the date and time of post. </p> 
      
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
  <div align="left">    
    <p class='text1'>News Title: 
        <input name="news_titlev" type="text" id="news_titlev" size="40"> 
        <br> 
        News:<br> 
        <textarea name="news_txtv" cols="40" rows="5" id="news_txtv"></textarea> 
        <br> 
        <input name="news_idv" type="hidden" id="news_idv"> 
        <input name="news_datev" type="hidden" id="news_datev"> 
        <br> 
        <input type="submit" name="action" value="Submit"> 
        </p> 
    </div> 
</form> 
<?PHP  
   } else { 
$dbcnx = mysql_connect("localhost", "root", "****") or die(mysql_error());
	mysql_select_db("intranet") or die(mysql_error());
  
// If news has been submitted, add it to the database.    
if (isset($_POST['action']) && $_POST['action'] == 'submit')  { 
   	$query = "INSERT INTO hhmnews SET news_id='".$_POST[news_idv]."', news_txt='".$_POST[news_txtv]."', news_date=NOW(), news_title='".$_POST[news_titlev]."'"; 
	$result = mysql_query($query) or die(mysql_error()); 
	
print("News Added!"); 
}
   echo ("<P>Here are all the news in our database:</P>"); 
   // Request the text of all the news  
   $result = mysql_query("SELECT * FROM hhmnews", $dbcnx); 
if ($rows = mysql_fetch_array($result)) { 
   do { 
   printf ("<i>%s</i><br>", $rows["news_date"]); 
   printf ("<b>%s</b><br>", $rows["news_title"]); 
   printf ("%s<p>", $rows["news_txt"]); 
   } while ($rows = mysql_fetch_array ($result)); 
//if no news found 
} else { 
   echo "<p class='text1'>No news where found at this time</p>"; 
   } 
    // When clicked, this link will load this page 
    // with the news submission form displayed. 
        echo '<A HREF="'. $_SERVER['PHP_SELF'] . '?addnews=1">Add NEWS</A></P>'; 
}
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

feyd wrote:but make sure to quote your named array indices! :D
:wink:
paquin1
Forum Commoner
Posts: 36
Joined: Fri Aug 06, 2004 1:57 pm

Post by paquin1 »

like I say... newbie... what are my named array indices?

news_id='".$_POST[news_idv]."

the [] or the news_id?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

news_idv, news_txtv, news_titlev to name a few... unless they're in double quoted strings.. and even then you might want to break them out of the string, or use "complex" inclusion:

Code: Select all

<?php

$somevar['foo'] = 'bar';
$string = "this is a string with {$somevar['foo']}.";

?>
Post Reply