dbconnect.php
<?php
$username = "punjabi_3";//your username
$password = "3";//your password
$host = "localhost";//your mySQL server
$database = "punjabi_3";//The name of the database your table is in
mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error());//connect, but if there is a problem, display an error message telling why there is a problem
mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error());//Choose the database from your mySQL server, but if there is a problem, display an error telling why
?>
editnews.php
<?
include("dbconnect.php"); //connect to the database
if(!isset($cmd)) //if the variable $cmd is not set, do this
{
//display all the news
$result = mysql_query("select * from mynews order by id DESC"); //select all posts from table mynews
while($r=mysql_fetch_array($result)) //for every row in the table, do this
{
extract($r);//remove the $r, so that its just the variable
//display each entry with links to edit and delete them
echo "<b><u>$title</b></u><br>".stripslashes($message)."<br>By $user<p><a href='editnews.php?cmd=edit&id=$id'><i>Edit</i></a> :: :: <a href='editnews.php?cmd=delete&id=$id'><i>Delete</i></a><br>";
echo "<br>";//so we have spacing between entries
}//end functions
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") //if the link for the edit cmd was selected, do this
{
if (!isset($_POST["submit"])) //if submit has not yet been pressed, do this
{
$id = $_GET["id"]; //get the id of the news article being edited. This is provided in the link to edit it.
$sql = "SELECT * FROM mynews WHERE id=$id"; //select all columns from mynews with this id
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form action="editnews.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
Title*:<br>
<INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>
Message*:<br>
<TEXTAREA NAME="message" rows=10 cols=43><? echo $myrow["message"] ?></TEXTAREA><br>
User*:<br>
<INPUT TYPE="TEXT" NAME="user" VALUE="<?php echo $myrow["user"] ?>" SIZE=30><br>
<br>
<hr>
<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="submit">
</form>
<? } ?>
<?
if ($_POST["$submit"]) //if submit was pressed, do this
{
$title = $_POST["title"];
$message = $_POST["message"];
$user = $_POST["user"];
$email = $_POST["email"];
$sql = "UPDATE mynews SET title='$title',message='$message',user='$user',email='$email' WHERE id=$id"; //UPDATE the columns in your table with the new content provided in the text areas in the form
$result = mysql_query($sql); //run this query
echo "Information updated. <a href=editnews.php>Return</a>"; //provide an easy link back to editnews.php
}
}
?>
<?
if($_GET["cmd"]=="delete")
{
$sql = "DELETE FROM mynews WHERE id=$id"; //DELETE the row that contains the $id of the post selected
$result = mysql_query($sql); //run query
echo "News Article Deleted!<br><a href=editnews.php>Return to Edit News</a>"; //easy link back to editnews.php
}
submit.php
<?php
include("./dbconnect.php");//include the file that connects to the database
if(!empty($title)) {//if the title is not empty, than do this function
$title = addslashes($title);
$user = addslashes($user);
$message = addslashes($message);
$date = date("F j, Y");//set up the date format
$date2 = mktime();
$sql = "INSERT INTO mynews (id, title, user, message, date) VALUES ('NULL', '$title','$user','$message','$date')";//Insert all of your information into the mynews table in your database
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
echo "Database Updated.";
} else {//However, if the title variable is empty, than do this function
?>
<form name="news" method="post" action="<?php echo $PHP_SELF; ?>">
<p>Please fill out all of the following fields:</p>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="117"><font size="1">News Topic/Title*: </font> </td>
<td width="577">
<font size="1">
<input type="text" name="title" size="50">
</font>
</td>
</tr>
<tr>
<td width="117"><font size="1">Username*:</font></td>
<td width="577">
<font size="1">
<input type="text" name="user" size="50">
</font>
</td>
</tr>
<tr>
<td width="117"><font size="1">Message*:</font></td>
<td width="577">
<font size="1">
<textarea name="message" rows=10 cols=43></textarea>
</font>
</td>
</tr>
</table>
<p>
<font size="1">
<input type="submit" name="Submit" value="Submit"></font>
</p>
</form> <?php
}//end this function
?>
viewnews.php
<center> <?
include("dbconnect.php");//include the file to connect to the database
$getnews = mysql_query("SELECT * FROM mynews ORDER BY id DESC");//query the database for all of the news
while($r=mysql_fetch_array($getnews)){//while there are rows in the table
extract($r);//remove the $r so its just $variable
echo("<hr>
<font size=3>$title added on $date</font><br>
<font size=1>Posted by $user</font><br>
<font size=2>$message</font><p>");
}
?>
</center>
ok this is the code for the script

what i am saying is it put the news in Tables insted like
http://www.desipeeps...ws/viewnews.php
Edited by jatt, 26 July 2005 - 04:00 AM.
