Return to Snippet

Revision: 44267
at April 8, 2011 21:30 by JonnySnip3r


Initial Code
<?php
// Connect to the server::
mysql_connect('localhost','jonno','password')
	or die('Sorry, an error occurred when connecting to the server.');

// Select the database::
mysql_select_db('carl_forum')
	or die ('Sorry, an error occurred when select the database.');
	
	if(isset($_POST['post']) && !empty($_POST['post_title']) && !empty($_POST['post_body']))
	{
		// Set up and clean our variables
		$title = strip_tags(mysql_real_escape_string($_POST['post_title']));
		$body = strip_tags(mysql_real_escape_string($_POST['post_body']));
				
		$sql = "INSERT INTO posts VALUES (NULL,'$title','$body')";
		if(mysql_query($sql))
		{
			echo 'Thanks! you have made a post!';
		}
		else
		{
			echo 'Damn! it failed to post.' . mysql_error();
		}
		
		
	}
?>

<html>
	<head>
		<title>Carl's Forum</title>
	</head>

	<body>
		
		<?php
		$sql = "SELECT * FROM posts ORDER BY post_id DESC";
		$query = mysql_query($sql);
		while($row = mysql_fetch_array($query)) :
		?>
		<h1><?php echo $row['post_title']; ?></h1>
		<p><?php echo $row['post_body']; ?></p>
		<?php endwhile; ?>
		
		<form method='post' action='#'>
			<input type='text' name='post_title' /><br />
			<textarea name='post_body' rows='10' cols='50'></textarea><br />
			<input type='submit' name='post' value='Post' />
		</form>
	</body>
</html>

Initial URL

                                

Initial Description
Just a simple php insert script.

Initial Title
Simple Database Insert

Initial Tags
database, sql, php, simple

Initial Language
PHP