A tutorial on how to make a script so you can make a system so you can update news from your webrowser.
Body
So you want a site that allows you to update news from your web browser like Cutenews, or Fusion, but want to make your own? Well then, read on! This tutorial will tell you exactly how to do so!
First off, I assume you know the basics of php/mysql. If you dont, then dont panic, for I will explain everything step by step to you!
Now, first you need to create your news table in MySQL to hold all your data. Make a new file and call it create.php. Enter the code as shown below:
//include the database connection file
include './database.php';
//Now create the table!
$create = "CREATE TABLE news (
author VARCHAR(20) NOT NULL,
email VARCHAR(20) NOT NULL,
title text NOT NULL,
avatar text NOT NULL,
news text NOT NULL )";
$result = mysql_query($create);
/*If the table could not be created, tell the user something went wrong */
if (!$result)
{
echo 'Couldnt create news table! >:O';
}
else
{
echo 'News table creation sucessfull!';
?>
Thats it for your creation file! Now let me break it all down for you.
include './database.php';
That includes your db connection information needed to create the table. Pretty self-explanitory :P.
$create = "CREATE TABLE news (
author VARCHAR(20) NOT NULL,
email VARCHAR(20) NOT NULL,
title text NOT NULL,
avatar text NOT NULL,
news text NOT NULL )";
Here, I created a variable which hold the table creation info for easier use if you wanted to call this funcion twice in the same script...which there isnt really a point in doing so...
$result = mysql_query($create);
Then I defined the variable $result to actually create the table.
//Check for errors
if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; }
if (!$news) { $errorList[$count] = "Invalid entry: News"; $count++; }
if (sizeof($errorList) == 0)
{
//If error check failed, then insert news into table
$query = "INSERT INTO news(title, avatar, news, author, email) VALUES('$title', '$pic', '$news', '$name', '$email' )";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
//News post sucessful! YAY
echo "New news post successful!";
}
else
{
//Errors occured :-/
echo "The following errors were encountered: ";
echo "
";
for ($x=0; $x
{
echo "
$errorList[$x]";
}
echo "
";
}
}
?>
Wow, you must be thinking, "WTF?! THIS IS WAY TO HARD FOR ME! SCREW THIS!". But wait just a minute! This is actually really REALLY simple! I wish I didnt have to, but just because I am a nice guy, I am going to explain it to you.