Page 1 of 2

Hide FORM to show results

Posted: Fri Aug 06, 2004 1:57 pm
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>';
}
?>

Posted: Fri Aug 06, 2004 2:00 pm
by feyd
try using $_GET['addnews'] instead of $addnews


$addnews would work ONLY if register_globals were on, which they shouldn't be.

Posted: Fri Aug 06, 2004 4:47 pm
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>';
}
?>

Posted: Fri Aug 06, 2004 4:50 pm
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)

?

Posted: Fri Aug 06, 2004 4:55 pm
by Joe
Damn tim you must have some powerful vision there lol. Well done!

Posted: Fri Aug 06, 2004 4:58 pm
by paquin1
I have no idea, but ok is gone.
I mean the period, but the form still does not work.
Thanks

Posted: Fri Aug 06, 2004 5:00 pm
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]."'";

Posted: Fri Aug 06, 2004 5:01 pm
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.

Posted: Fri Aug 06, 2004 5:39 pm
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.

Posted: Fri Aug 06, 2004 5:40 pm
by feyd
but make sure to quote your named array indices! :D

Posted: Fri Aug 06, 2004 6:25 pm
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());

Posted: Fri Aug 06, 2004 7:03 pm
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>'; 
}
?>

Posted: Fri Aug 06, 2004 7:05 pm
by tim
feyd wrote:but make sure to quote your named array indices! :D
:wink:

Posted: Fri Aug 06, 2004 7:08 pm
by paquin1
like I say... newbie... what are my named array indices?

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

the [] or the news_id?

Posted: Fri Aug 06, 2004 7:21 pm
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']}.";

?>