Adding two records to database

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

Post Reply
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Adding two records to database

Post by tail »

In function add_news(), it adds the form information and a blank row:

Code: Select all

<?php
$status = array();

$status_messages['success'] = 'Your message has been posted.';
$status_messages['error_mysql_query'] = 'Unable to send the query.';
$status_messages['error_mysql_db'] = 'Unable to connect to the databse.';
$status_messages['error_mysql_connect'] = 'Unable to connect to the server.';

$database['user'] = 'perks';
$database['password'] = '*****';
$database['database'] = 'perks';

echo "<center><span class='title'>Perks's Page - News CMS</span></center><br><br>";

function news_form()
{
echo "<center";
echo "<form action='index.php?id=admin&page=news' method='post'>";
echo "<input type='hidden' name='form_mode' value='show_news'>";
echo "<input type='submit' value='Show News' class='button'>";
echo "</form>";
echo "<form action='index.php?id=admin&page=news' method='post'>";
echo "<input type='hidden' name='form_mode' value='add_news'>";
echo "<input type='submit' value='Add News' class='button'>";
echo "</form>";
echo "</center>";
}

function add_news()
{
echo "<form action='index.php?id=admin&page=news' method='post'>";
echo "<input type='hidden' name='form_mode' value='add_news'>";
echo "Subject: <input type='text' name='subject' class='button'><br>";
echo "News Body: <br><textarea cols='40' rows='10' name='body' class='button'></textarea><br><br>";
echo "<input type='submit' value='Submit' class='button'>";
echo "</form>";
global $status;
global $database;
	$connection = mysql_connect(localhost,$database['user'],$database['password']);
	if($connection)
	{
		$select_db = mysql_select_db($database['database'],$connection);
		if($select_db)
		{
			$subject=$_POST["subject"];
			$body=$_POST["body"];
			$date=date("n/j/y");
			$time=date("g:i A");
			$query_string = "INSERT INTO news VALUES('','$subject','$body','$date','$time')";
			$query = mysql_query($query_string);
			if($query)
			{
				$status[] =  $status_messages['success'];
			}
			else
			{
				$status[] =  $status_messages['error_mysql_query'];				
			}
		}
		else
		{
			$status[] =  $status_messages['error_mysql_db'];
		}
	}
	else
	{
		$status[] =  $status_messages['error_mysql_connect'];
	}
}
function show_news()
{

global $status;
global $database;

	$connection = mysql_connect(localhost,$database['user'],$database['password']);
	if($connection)
	{
		$select_db = mysql_select_db($database['database'],$connection);
		if($select_db)
		{
			$query_string = "SELECT * FROM news order by id DESC";
			$query_result = mysql_query($query_string);
			if($query_result)
			{
				$GLOBALS['count_news'] = mysql_numrows($query_result);
				echo "<center><span class='title'>News</span><br>";
				echo '' . $GLOBALS['count_news'] . ' news posts<br/><br><br>';
				if($GLOBALS['count_news'] > 0)
				{

for($i=0;$i<$GLOBALS['count_news'];$i++)
{

//echo 'processed a shout!<br/>';

//if($thisrow = mysql_fetch_array($query_result))
//{
//	$name = $thisrow['name'];
//	$message = $thisrow['message'];
//	$date = $thisrow['date'];
//	$time = $thisrow['time'];
//}
//else
//{
//	echo 'dang.';
//}


$subject=mysql_result($query_result,$i,"subject");
$body=mysql_result($query_result,$i,"body");
$date=mysql_result($query_result,$i,"date");
$time=mysql_result($query_result,$i,"time");

$GLOBALS['news_string'] .= <<<EOD
<font face='Verdana' size='1'><center><table border='1' width='500px'><tr><td colspan='2'><b>Subject: $subject</b></td></tr><tr><td colspan='2'>Body:<br>$body</td></tr><tr><td>Posted on: $date at $time</td><td>Posted by: Perks</td></tr></table></center></font><br><br>

EOD;

}

				}
				else
				{
					$GLOBALS['news_string'] = 'There are no news posts';
				}
				return $GLOBALS['news_string'];
			

			}
			else
			{
				$status[] =  $status_messages['error_mysql_query'];				
			}
		}
		else
		{
			$status[] =  $status_messages['error_mysql_db'];
		}
	}
	else
	{
		$status[] =  $status_messages['error_mysql_connect'];
	}

	mysql_close($connection);
}

if(isset($_POST['form_mode']))
{
	if($_POST['form_mode'] == 'add_news')
	{
		add_news();
	}
}

if(isset($_POST['form_mode']))
{
	if($_POST['form_mode'] == 'show_news')
{
for($x=0;$x<count($GLOBALS['status']);$x++)
{
	echo $GLOBALS['status'][$x];
}
echo show_news();
}
}
if($_POST['form_mode'] == '')
{
	news_form();
}

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The code looks clean, so the only other explanation I can think of is that the function is getting called twice. The empty row is because the data that is going in is POST data, and if POST is not set, the inserted values would be empty.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

add_news() does get called twice. Once when you click "add news" on the news_form(), and again when you click "Submit" in the add_news() output.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Haha wow that was stupid :roll: I guess I need some caffeine, getting a little late here. This worked:

Code: Select all

<?php
$status = array();

$status_messages['success'] = 'Your message has been posted.';
$status_messages['error_mysql_query'] = 'Unable to send the query.';
$status_messages['error_mysql_db'] = 'Unable to connect to the databse.';
$status_messages['error_mysql_connect'] = 'Unable to connect to the server.';

$database['user'] = 'perks';
$database['password'] = 'astra';
$database['database'] = 'perks';

echo "<center><span class='title'>Perks's Page - News CMS</span></center><br><br>";

function news_form()
{
echo "<center";
echo "<form action='index.php?id=admin&page=news' method='post'>";
echo "<input type='hidden' name='form_mode' value='show_news'>";
echo "<input type='submit' value='Show News' class='button'>";
echo "</form>";
echo "<form action='index.php?id=admin&page=news' method='post'>";
echo "<input type='hidden' name='form_mode' value='add_news'>";
echo "<input type='submit' value='Add News' class='button'>";
echo "</form>";
echo "</center>";
}
function add_news()
{
		echo "<form action='index.php?id=admin&page=news' method='post'>";
		echo "<input type='hidden' name='form_mode' value='addthenews'>";
		echo "Subject: <input type='text' name='subject' class='button'><br>";
		echo "News Body: <br><textarea cols='40' rows='10' name='body' class='button'></textarea><br><br>";
		echo "<input type='submit' value='Submit' class='button'>";
		echo "</form>";
}
function addthenews()
{
global $status;
global $database;
	$connection = mysql_connect(localhost,$database['user'],$database['password']);
	if($connection)
	{
		$select_db = mysql_select_db($database['database'],$connection);
		if($select_db)
		{
			$subject=$_POST["subject"];
			$body=$_POST["body"];
			$date=date("n/j/y");
			$time=date("g:i A");
			$query_string = "INSERT INTO news VALUES('','$subject','$body','$date','$time')";
			$query = mysql_query($query_string);
			if($query)
			{
				$status[] =  $status_messages['success'];
			}
			else
			{
				$status[] =  $status_messages['error_mysql_query'];				
			}
		}
		else
		{
			$status[] =  $status_messages['error_mysql_db'];
		}
	}
	else
	{
		$status[] =  $status_messages['error_mysql_connect'];
	}
}
function show_news()
{

global $status;
global $database;

	$connection = mysql_connect(localhost,$database['user'],$database['password']);
	if($connection)
	{
		$select_db = mysql_select_db($database['database'],$connection);
		if($select_db)
		{
			$query_string = "SELECT * FROM news order by id DESC";
			$query_result = mysql_query($query_string);
			if($query_result)
			{
				$GLOBALS['count_news'] = mysql_numrows($query_result);
				echo "<center><span class='title'>News</span><br>";
				echo '' . $GLOBALS['count_news'] . ' news posts<br/><br><br>';
				if($GLOBALS['count_news'] > 0)
				{

for($i=0;$i<$GLOBALS['count_news'];$i++)
{

//echo 'processed a shout!<br/>';

//if($thisrow = mysql_fetch_array($query_result))
//{
//	$name = $thisrow['name'];
//	$message = $thisrow['message'];
//	$date = $thisrow['date'];
//	$time = $thisrow['time'];
//}
//else
//{
//	echo 'dang.';
//}


$subject=mysql_result($query_result,$i,"subject");
$body=mysql_result($query_result,$i,"body");
$date=mysql_result($query_result,$i,"date");
$time=mysql_result($query_result,$i,"time");

$GLOBALS['news_string'] .= <<<EOD
<font face='Verdana' size='1'><center><table border='1' width='500px'><tr><td colspan='2'><b>Subject: $subject</b></td></tr><tr><td colspan='2'>Body:<br>$body</td></tr><tr><td>Posted on: $date at $time</td><td>Posted by: Perks</td></tr></table></center></font><br><br>

EOD;

}

				}
				else
				{
					$GLOBALS['news_string'] = 'There are no news posts';
				}
				return $GLOBALS['news_string'];
			

			}
			else
			{
				$status[] =  $status_messages['error_mysql_query'];				
			}
		}
		else
		{
			$status[] =  $status_messages['error_mysql_db'];
		}
	}
	else
	{
		$status[] =  $status_messages['error_mysql_connect'];
	}

	mysql_close($connection);
}

if(isset($_POST['form_mode']))
{
	if($_POST['form_mode'] == 'add_news')
	{
		add_news();
	}
}

if(isset($_POST['form_mode']))
{
	if($_POST['form_mode'] == 'show_news')
{
for($x=0;$x<count($GLOBALS['status']);$x++)
{
	echo $GLOBALS['status'][$x];
}
echo show_news();
}
}
if($_POST['form_mode'] == '')
{
	news_form();
}
if(isset($_POST['form_mode']))
{
	if($_POST['form_mode'] == 'addthenews')
	{
		addthenews();
		news_form();
	}
}
?>
Post Reply