Basic News

By Earl on Jan 12, 2006

Create all three files, input the php code into each file that is listed. Then change the database info in ALL three files.



$host = "localhost";


$user = "your-username";


$pass = "your-password";


$dbname = "your-username_your-database";



DO NOT edit $conn = mysql_connect($host, $user, $pass);




Found an error? Leave a comment :-)



Feel free to edit or add some addons, However I'm not gonna help with ANY addons.

FIRST Creating table:
File name: 'createtable.php'

<?php
$host = "localhost";
$user = "your-username";
$pass = "your-password";
$dbname = "your-username_your-database";

$conn = mysql_connect($host, $user, $pass);
mysql_select_db($dbname);

mysql_query("DROP TABLE news"); /* ADDED: just in case */

$query = "CREATE TABLE `news` (`id` int(10) unsigned NOT NULL auto_increment, `news` TEXT NOT NULL default '', PRIMARY KEY (`id`))";

mysql_query($query);
mysql_close($conn);
?>

Run it once!

SECOND Process Page:
File name: 'index.php'

<?php
/* isset() is used to know if the variable exists */
/* $_POST array is created/updated after a submit */
if (isset($_POST['News']))
{
$host = "localhost";
$user = "your-username";
$pass = "your-password";
$dbname = "your-username_your-database";

$conn = mysql_connect($host, $user, $pass);
mysql_select_db($dbname);

$query = "INSERT !NTO news VALUES (NULL, '".$_POST['News']."')";
mysql_query($query) or die ("formInput error: $query");
mysql_close($conn);
}
?>

<!-- HTML CODE -->
<html>
<head>
<title>Index</title>
</head>

<body>
<h1>Index</h1>
<form name="New" action="./index.php" method="post">
<textarea name="News" cols="45" rows="10"></textarea>
<br><input type="submit" value="Submit">
</body>
</html>

THIRD News Section:
File name: 'news.php'

<?php
$host = "localhost";
$user = "your-username";
$pass = "your-password";
$dbname = "your-username_your-database";

$conn = mysql_connect($host, $user, $pass);
mysql_select_db($dbname);

$query = "SELECT * FROM news";
$result = mysql_query($query);

$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$return[$i] = $row["news"];
$i++;
}

mysql_free_result($result);
mysql_close($conn);
?>

<!-- HTML CODE -->
<html>
<head>
<title>News Section</title>
</head>

<body>
<h1>News Section</h1>
<?php
for ($i = 0; $i < count($return); $i++)
{
?>
<p><?= $return[$i] ?></p>
<?php
}
?>
</body>
</html>

Comments

Sign in to comment.
dma   -  Feb 10, 2016

wheres the script???
comments for what?

 Respond  
Earl   -  Jan 24, 2007

yes, no comments indeed.

 Respond  
BackoffJackson   -  Jan 23, 2007

Yes, no comments.

 Respond  
Earl   -  Jan 15, 2006

no comments? :(

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.