WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Creating A News Script For Your Site
Categories : PHP, Databases, MySQL Click here to Update Your Picture
Nick Wilson
Date : Jun 01st 2004
Grade : 3 of 5 (graded 6 times)
Viewed : 8259
File : 3900.zip
Images : No Images for this code example.
Search : More code by Nick Wilson
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

This script is just a basic news script. Many more features could be added to make it more secure and whatnot, but I'm just going over the basics for the time being. Included are four files:
news.php (view news)
addnews.php (add news)
editnews.php (edit news)
deletenews.php (delete news)

Feel free to modify this however you may wish. All pages include the HTML to make things easier on your part. At the beginning is the news.sql file used to create the news table (use this in phpMyAdmin or whatever your program of choice is). Also, for the sake of simplicity, news.php will also serve as the place where we will select which articles we want to edit or delete. You can easily modify this later on by, say, making the links only show when the user is logged in as an admin.


news.sql
CREATE TABLE `news` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL default '',
  `author` varchar(255) NOT NULL default '',
  `date` int(14) NOT NULL default '0',
  `body` text NOT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;




news.php
<html>
<head>
<title>News</title>
</head>
<body>
<hr>
<?php
// If No News ID Is Set
if (!isset($_GET[`id`])) {

// Grabbing All The News From The Table
$query2 = mysql_query("SELECT * FROM news");
$query = mysql_fetch_array($query2);

// Converts Date In The MySql Table To Correct Format
$date2 = $query[`date`];
$year = substr($date2, 0, 4);
$month = substr($date2, 4, 2);
$day = substr($date2, 6, 2);
$date = "$month-$day-$year";
?>
<b><? print $query['title']; ?></b><br />
<i>By <? print $query['author']; ?></i><br />
<i>Added On <? print $date; ?></i>
<br /><br />
<? print $query['article']; ?>
<br /><br />
<a href="editnews.php?id=<? print $query['id']; ?>">Edit Article</a>  <a href="deletenews.php?id=<? print $query['id']; ?>">Delete Article</a>
<hr>
<?
// If The News ID Is Set
} else if (isset($_GET['id'])) {
 
 
$id = $_GET['id'];
 
 
//Grabs Only The News Article With That ID
 
$query = mysql_query("SELECT * FROM news WHERE id='$id'");
?>
<b><? print $query['title']; ?></b><br />
<i>By <? print $query['author']; ?></i><br />
<i>Added On <? print $date; ?></i>
<br /><br />
<? print $query['article']; ?>
<br /><br />
<a href="editnews.php?id=<? print $id; ?>">Edit Article</a>  <a href="deletenews.php?id=<? print $id; ?>">Delete Article</a>
<hr>
<? } ?>
</body>
</html>



addnews.php
<html>
<head>
<title>Add News</title>
</head>
<body>
<?
// Displaying Any Errors Sent From The Add Subpage
if (isset($_GET['error'])) {
  if (
$_GET['error'] == '1') {
    print
'<font color="#FF0000"><b>ERROR: </b>You didn't fill in all of the fields. Please try again.</font>';
  }
  print '
<br /><br />';
}
?>
<form method="post" action="addnews.php?subpage=add">
Title:<br />
<input type="text" name="title">
<br /><br />
Author:<br />
<input type="text" name="author">
<br /><br />
Article:<br />
<textarea name="article" cols="40" rows="10"></textarea>
<br /><br />
<input type="submit" value="Submit">  <input type="reset" value="Clear">
</form>
</body>
</html>
<?
// If The URL Points To The Subpage Add
if (isset($_GET['
subpage']) && $_GET['subpage'] == 'add') {
 
  $mysqlhost = "localhost";    // Localhost is default
  $mysqluser = "";    // MySql Username
  $mysqlpass = "";    // MySql Password
  $mysqldb = ""    // MySql Database
 
  // Connecting To The Database
  $dbh = mysql_connect ($mysqlhost, $mysqluser, $mysqlpass) or die ('
Couldn't connect to database.');
 
mysql_select_db ($mysqldb);
 
 
// Checking For Errors
 
if (isset($_POST['title']) && isset($_POST['author']) && isset($_POST['article'])) {
   
   
$date = date(Ymd);    // Date For Our Article
   
    // Add News To Database
   
$query = mysql_query("INSERT INTO news SET title='$title', author='$author', date='$date', article='$article'");

   
// News Added Message
   
print 'News successfully added.';
    print
'<br /><br />';
    print
'<a href="news.php">Continue</a>';
   
  } else {
   
header("Location: addnews.php?error=1")    // Redirects Us Back To The Add News Page With The Error
 
}
}
?>



editnews.php
<html>
<head>
<title>Edit News</title>
</head>
<body>
<?
// Displaying Any Errors Sent From The Add Subpage
if (isset($_GET['error'])) {
  if (
$_GET['error'] == '1') {
    print
'<font color="#FF0000"><b>ERROR: </b>You didn't fill in all of the fields. Please try again.</font>';
  }
  print '
<br /><br />';

// Making Sure That An ID Is Set
} else if (isset($_GET['
id'])) {

  // Grabbing News From Database With That ID
  $query2 = mysql_query("SELECT * FROM news WHERE id=`$id`");
  $query = mysql_fetch_array($query2);
?>
<form method="post" action="editnews.php?subpage=update">
Title:<br />
<input type="text" name="title" value="<? print $query['
title']; ?>">
<br /><br />
Author:<br />
<input type="text" name="author" value="<? print $query['
author']; ?>">
<br /><br />
Article:<br />
<textarea name="article" cols="40" rows="10"><? print $query['
article']; ?></textarea>
<br /><br />
<input type="submit" value="Submit">  <input type="reset" value="Clear">
</form>
</body>
</html>
<? } else { ?>
<font color="#FF0000"><b>ERROR: </b>No ID is set. Please <a href="javascript:history.go(-1)">go back</a> and try again.</font>
<? }

// If The URL Points To The Subpage Update
if (isset($_GET['
subpage']) && $_GET['subpage'] == 'update') {
 
  $mysqlhost = "localhost";    // Localhost is default
  $mysqluser = "";    // MySql Username
  $mysqlpass = "";    // MySql Password
  $mysqldb = ""    // MySql Database
 
  // Connecting To The Database
  $dbh = mysql_connect ($mysqlhost, $mysqluser, $mysqlpass) or die ('
Couldn't connect to database.');
 
mysql_select_db ($mysqldb);
 
 
// Checking For Errors
 
if (isset($_POST['title']) && isset($_POST['author']) && isset($_POST['article'])) {
   
   
// Updating News
   
$query = mysql_query("UPDATE news WHERE id='$id' SET title='$title', author='$author', date='$date', article='$article' LIMIT 1");
   
   
// News Updated Message
   
print 'News successfully updated.';
    print
'<br /><br />';
    print
'<a href="news.php">Continue</a>';
   
  } else {
   
header("Location: editnews.php?error=1")    // Redirects Us Back To The Edit News Page With The Error
 
}
}
?>



deletenews.php
<?php
// Making Sure A News ID Is Set
if (isset($_GET['id'])) {
 
 
$id = $_GET['id'];
 
 
// If No Subpage Is Set, We Go To A Confirmation Message
 
if (!isset($_GET['subpage'])) {
    print
'Are you sure you want to delete this news?';
    print
'<br /><br />';
    print
'<a href="deletenews.php?subpage=delete&id=<? print $id; ?>">Yes</a> | <a href="news.php">No</a>';
   
 
// If The Subpage Is Set To Delete
 
} else if (isset($_GET['subpage']) && $_GET['subpage'] == 'delete') {
   
   
//Deleting The News
   
$query = mysql_query("DELETE FROM news WHERE id='$id' LIMIT 1");
   
   
// News Deleted Message
   
print 'News deleted.';
    print
'<br /><br />';
    print
'<a href="news.php">Continue</a>';
  }
} else {
 
 
// Error When No ID Is Set
 
print '<font face="Arial"><b>ERROR: </b>No ID is set. Please <a href="javascript:history.go(-1)">go back</a> and try again.</font>';
 
}
?>


Hopefully, this script has helped you make your site better. I tried my best to tell what everything in this script does to make things a bit easier., so hopefully that helped you better understand this script. Feel free to use this on your site. If you have any questions regarding the script, please contact me or leave a comment and I'll try to respond when I get a chance.



bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Invision Forums Latest Threads list
Categories : PHP, Miscellaneous, Databases, MySQL
AJAX Data Grid System using php and mysql. A complete login system with the ability to display data in a grid using ajax. Add , update and delete the records without reloading the page.
Categories : PHP, AJAX, Databases, MySQL, Java Script
Tropicalm Genetree Family (MySQL based family tree)
Categories : PHP, Interfaces, Databases, MySQL, Complete Programs
GroupIT Engine v1.00rc1
Categories : PHP, Content Management, MySQL, Databases
mySQL/PHP/search with multientry form and table output with colored rows
Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases
Email a user with out exposing email address
Categories : PHP, Databases, MySQL, Email
Creating thumbnails from MySQL Blobs online
Categories : PHP, MySQL, Graphics, HTML and PHP, Databases
[PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more.
Categories : PHP, PHP Classes, Databases, MS SQL Server, MySQL
complete, simple, working example of a login screen/system using php functions, cookies, and a mysql database for begginers.
Categories : Authentication, Complete Programs, PHP, MySQL, Databases
color codes for positive and negative numbers
Categories : PHP, MySQL, Databases, HTML
Simple function to return the number of days in a time span between 2 given dates.
Categories : PHP, Date Time, MySQL, Databases
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
A login page that require username, password and userlevel.
Categories : PHP, Security, Sessions, MySQL, Databases
 Nick Wilson wrote :1118
Well, looks like I made a few mistakes in this example. First of all is this part from news.php:

&lt;?
// Converts Date In The MySql Table To Correct Format
$date2 = $news[`date`]; 
$year = substr($date2, 0, 4); 
$month = substr($date2, 4, 2); 
$day =  substr($date2, 6, 2); 
$date = "$month-$day-$year";

// If No News ID Is Set
if (!isset($_GET[`id`])) {

  // Grabbing All The News From The Table
  $query = mysql_query("SELECT * FROM news");
?&gt;

Instead, it should be like this:

&lt;?
// If No News ID Is Set
if (!isset($_GET[`id`])) {

  // Grabbing All The News From The Table
  $query2 = mysql_query("SELECT * FROM news");
  $query = mysql_fetch_array($query2);

  // Converts Date In The MySql Table To Correct Format
  $date2 = $query[`date`]; 
  $year = substr($date2, 0, 4); 
  $month = substr($date2, 4, 2); 
  $day =  substr($date2, 6, 2); 
  $date = "$month-$day-$year";
?&gt;

Next is this part from editnews.php:

  // Grabbing News From Database With That ID
  $query = mysql_query("SELECT * FROM news WHERE id=`$id`");

This should be instead:

  // Grabbing News From Database With That ID
  $query2 = mysql_query("SELECT * FROM news WHERE id=`$id`");
  $query = mysql_fetch_array($query2);

And finally, this part from deletenews.php:

    //Deleting The News
    $query = mysql_query("DELETE * FROM news WHERE id=`$id` LIMIT 1");

Should be instead:

    //Deleting The News
    $query = mysql_query("DELETE FROM news WHERE id=`$id` LIMIT 1");

This should make the script run if it isn`t working right (even if it is, upgrade anyway, as version 4.3.6 introduces a much stricter level of coding as I found out the hard way, hence the modifications above). Hope this helps.