Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2007, 06:41 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
The CMS (#3) PHP

Ok, so we are now onto part 3 of our tutorial!

So in this we are going to do:

add-news.php

This is going to be quite a complicated one, so make sure that your watching!

Let's start off with creating the database tables that we need!

First thing you need to do is edit your install.php and add the following code, or if your not using an installer place the following code into whatever your using.

Code:
CREATE TABLE `news` (
`ID` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 250 ) NOT NULL ,
`author` VARCHAR( 250 ) NOT NULL ,
`date` VARCHAR( 250 ) NOT NULL ,

`content` TEXT NOT NULL ,
PRIMARY KEY ( `ID` )
) TYPE = MYISAM

Or your install.php should now look like this:


PHP Code:
<?php
include("../includes/connect.php");

// Create a MySQL table in the selected database
mysql_query("CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
 username VARCHAR(12), 
 pw VARCHAR(32))"
)
 or die(
mysql_error());  

    echo 
"Admin users table created!";

mysql_query("CREATE TABLE `news` (
`ID` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 250 ) NOT NULL ,
`author` VARCHAR( 250 ) NOT NULL ,
`date` VARCHAR( 250 ) NOT NULL ,

`content` TEXT NOT NULL ,
PRIMARY KEY ( `ID` )
) TYPE = MYISAM"
)
 or die(
mysql_error());
 
     echo 
"News table created!";

?>
Right then, now we have created the tables, we need to create our add-news.php

PHP Code:
<?php
session_start
();
if(
$login_username=="") {
Header("Location: login.php");
} else {
//The following code is the PHP form handling application.
//Connecting to the MySQL database
include("../includes/connect.php");

//The current date.
$thedate date('n/j/Y');


if(
$_POST['submit']) {
//Simplifying the variables.
$title $_POST['title'];
$author $_POST['author'];
$content $_POST['content'];

//htmlspecialchars() converts special characters into HTML entities.
$title htmlspecialchars($title);
$author htmlspecialchars($author);

//The MySQL query which will insert content into the table.
$query "INSERT INTO news (ID, title, author, date, content) VALUES ('', '$title', '$author', '$thedate', '$content')";
//Executing the query with mysql_query().
mysql_query($query) or die(mysql_error());
echo 
"<center><strong>News item added!</strong></center>";
}
?>
<form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>">
<table align="center">
<tr><td align="right">Title:</td><td><input type="text" name="title" maxlength="250" /></td></tr>
<tr><td align="right">Author:</td><td><input type="text" name="author" maxlength="250" /></td></tr>


<tr><td align="right">Content:</td><td><textarea name="content" cols="50" rows="10"></textarea></td></tr>
<tr><td> </td><td><input type="submit" name="submit" value="Submit" /><input type="reset" name="reset" value="Reset" /></td></tr>
</table>
</form>
<?php ?>
Right then, so lets get at this line by line!

First few lines our are security to make sure no un autorised users can add news

So the first line you won't know is this:

$thedate = date('n/j/Y'); - Now this is the PHP date function, this is usefull, this gets the server date!

If you want to know more about this function, because i am not going to explain it in this tutorial go here they explain it nicely

if($_POST['submit']) { - You know that to post a form we do this code

Code:
<input type="submit" name="Submit" value="Submit">
welll this bit: type="submit" means that we are now checking the following:

"If sumit has been posted then do the following:"

$title = $_POST['title'];
$author = $_POST['author'];
$content = $_POST['content'];

- Getting the information from the title, author and content fields.


$title = htmlspecialchars($title);
$author = htmlspecialchars($author);

Now this is somthing you will use allot in forms, this means that any special charicters such as <iframe or stuff like that is removed, so that it won't show it.

Read more about them here

$query = "INSERT INTO news (ID, title, author, date, content) VALUES ('', '$title', '$author', '$date', '$content')";

So I am going to do the code to speach thing again:

"In the variable $query insert into the table news into the rows called ID, title, author, date & content the following variables '' nothing, because id is auto increment we don't have to add anything then the variable $title into the table row, then author into its corresponding row, and so on and so on!"

mysql_query($query) or die(mysql_error()); - Actually runs the above query, or it gives you an error.

echo "<center><strong>News item added!</strong></center>"; - tells the user their news is added.

The only line you may not know in the form code is this bit

PHP Code:
<form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>">
echo $_SERVER[PHP_SELF]; - Means that its going to echo the same page again, so now! this is the clever bit..


You remember at the top we put: if($_POST['submit']) {

Right look at the bottom of our HTML code.

<input type="submit" name="submit" value="Submit" /> :O:O:O:O:O

Shocking!!!

That means that its only going to execute that PHP code if somoene has clicked submit!!!!

Clever stufff egh..

<?php } ?> - Just ending our security

So there we go, in a few hours i will post the follow up tutorial for the edit-news.php which include del function!

Watch out for that peeps

Base

(No up to the minute fule included in this post, because i am posting the next tutorial very soon, and am making some changes so no point!)

__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.

Last edited by Base; 03-31-2007 at 06:59 AM.
Reply With Quote
  #2 (permalink)  
Old 04-25-2007, 08:19 PM
Junior Member
 
Join Date: Apr 2007
Posts: 3
surgeboard is on a distinguished road
So far so good, and im understanding the concept. Great stuff man.
Reply With Quote
  #3 (permalink)  
Old 05-19-2007, 02:15 PM
Junior Member
 
Join Date: May 2007
Location: Croatia
Posts: 20
Jabba is on a distinguished road
Send a message via MSN to Jabba
this is really nice tut!

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 02:09 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com