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
 

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!”;

}}

?>

“form1” method=“post” action=“”>

“width:600px; margin:auto;”>

Title:

Description:

Keyword:

Category:

Content:

“submit” name=“Submit” value= style=“color: #08322A; float: right; margin: 0px 254px;padding: 1px 11px;”>


Output

submission form

And the next process is the article review and to create a file article.php and write the review code.

Example

include‘connect.php’;

$query=“SELECT * FROM article”;

$result=mysql_query($query);

?>

while($rows=mysql_fetch_array($result)){

?>

“width:600px; margin:auto;”>“edit.php?id=“>Edit “delete.php?id=“> “post.php”>Post

“width:600px; margin:auto;”>

Title:

‘title’]; ?>

Description:

‘descr’]; ?>

Keyword:

‘keyword’]; ?>

Content:

‘content’]; ?>

}

?>

mysql_close();

?>

Output

Click on this link and view your article.

click on link and view your article

Your article review.

article review

Then you will create am “Edit” and “Update” function and first an “Edit.php” file, such as the following.

Example

include‘connect.php’;

$id=$_GET[‘id’];

$query=“SELECT * FROM article WHERE id=’$id'”;

$result=mysql_query($query);

$rows=mysql_fetch_array($result);

?>

“form1” method=“post” action=“update.php”>

“width:600px; margin:auto;”>

Title:

Description:

Keyword:

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:

article editing

 

Open your editor form and change your content here such as in the following:

change your article content

error

 

If you are editing an article then given a reason why you are editing it, such as in the following:

updated your article

 

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

view updated data

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

delete article

successful delete massege

Prepared by Ollala Corp

You might also like More from author

Leave A Reply

Your email address will not be published.