Post Article On Site With Update And Delete Functions In PHP – Info PHP
Introduction
This article explains how to post articles on a site and if you want to update or delete an article then you can also do these operations in PHP. Let’s begin creating it. You will first need a table for storing article contents and then you will create a connection with your table.
This is your article table.
And the next step is your connection file for a connection with your database.
$hostname=“localhost”;
$username=“root”;
$password=“”;
$database_name=“demo”;
mysql_connect(“$hostname”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$database_name”)or die(“cannot select DB”);
?>
Then you will create a “post.php” file and write code for posting the article and store the content into the table.
Example
>
textarea{
border: 1px solid #ECCCCE;
padding: 2px;
}body{
color: #408800;
}
ini_set(‘display_errors’,0);
include‘connect.php’;
if(isset($_POST[‘Submit‘])){
$title=$_POST[‘title’];
$descr=$_POST[‘descr’];
$keyword=$_POST[‘keyword’];
$content=$_POST[‘content’];
$category=$_POST[‘category’];
$query=“INSERT into article values(”,’$title’,’$descr’,’$keyword’,’$content’,”,’$category’)”;
$result=mysql_query($query)or die(mysql_error());
if($result){
echo “Successful Post your article…”;
echo “
“;
echo “View Your post artcile”;
}
else
{
echo “Error in your sql query please check again!”;
}}
?>
method=“post” action=“”>
Title:
id=“title” cols=“40” rows=“2”>
Description:
id=“descr” cols=“40” rows=“2”>
Keyword:
id=“keyword” cols=“40” rows=“2”>
Category:
>Please select category
>php
>ASP
>JSP
>iPHONE
Content:
id=“content” cols=“40” rows=“10”>
“submit” name=“Submit” value=“Post” style=“color: #08322A; float: right; margin: 0px 254px;padding: 1px 11px;”>
Output
And the next process is the article review and to create a file article.php and write the review code.
Example
>
textarea{
border: 1px solid #ECCCCE;
padding: 2px;
}body{
color: #69928E;
}strong{
color:#333333;
font-weight: normal;
}
include‘connect.php’;
$query=“SELECT * FROM article”;
$result=mysql_query($query);
?>
while($rows=mysql_fetch_array($result)){
?>
Title:
‘title’
]; ?>
Description:
‘descr’
]; ?>
Keyword:
‘keyword’
]; ?>
Content:
‘content’
]; ?>
}
?>
mysql_close();
?>
Output
Click on this link and view your article.
Your article review.
Then you will create am “Edit” and “Update” function and first an “Edit.php” file, such as the following.
Example
>
textarea{
border: 1px solid #ECCCCE;
padding: 2px;
}body{
color: #408800;
}
include‘connect.php’;
$id=$_GET[‘id’];
$query=“SELECT * FROM article WHERE id=’$id'”;
$result=mysql_query($query);
$rows=mysql_fetch_array($result);
?>
method=“post” action=“update.php”>
Title:
id=“title” cols=“40” rows=“2”>‘title’]; ?>
Description:
id=“descr” cols=“40” rows=“2”>‘descr’]; ?>
Keyword:
id=“keyword” cols=“40” rows=“2”>‘keyword’]; ?>
Content:
id=“content” cols=“40” rows=“10”>‘content’]; ?>
Type reason:
“text”
name=“reason” id=“reason” value=““>
“id” type=“hidden” id=“id” value=““>
“submit” name=“Submit” value=“Update” style=“color: #08322A; float: right; margin: -44px 254px;padding: 1px 11px;”>
mysql_close();
?>
Click on the Edit link to edit your article as in the following:
Open your editor form and change your content here such as in the following:
If you are editing an article then given a reason why you are editing it, such as in the following:
And next is the “Update.php” file, such as in the following:
Example
ini_set(‘display_errors’,0);
include‘connect.php’;
//query with sql
if(!empty($_POST[‘reason‘])){
$query=“UPDATE article SET
title=‘”.mysql_real_escape_string($_POST[‘title‘]).”‘,
descr=‘”.mysql_real_escape_string($_POST[‘descr‘]).”‘,
keyword=‘”.mysql_real_escape_string($_POST[‘keyword‘]).”‘,
content=‘”.mysql_real_escape_string($_POST[‘content‘]).”‘, reason=‘”.mysql_real_escape_string($_POST[‘reason‘]).”‘ WHERE id=‘”.mysql_real_escape_string($_POST[‘id‘]).”‘”;
$result=mysql_query($query)or die(mysql_error());
if($result){
echo “Successful Update your record…”;
echo “
“;
echo “View Your updated data”;
}
else
{
echo “Error in your sql query please check again!”;
}}else{
echo ‘please type the reason’;
}
?>
Output
And finally you will create a “delete.php” file for deleting an article, such as in the following:
Example
ini_set(‘display_errors’,0);
include‘connect.php’;
$query=“DELETE FROM article WHERE id='”.$_GET[‘id’].“‘”;
$result= mysql_query($query)or die(mysql_error());
if($result){
echo “Successful delete your record…”;
echo “
“;
echo “View Your updated data”;
}
else
{
echo “Error in your sql query please check again!”;
}
?>
Output
Article Prepared by Ollala Corp