Post Article On Site With Update And Delete Functions In 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.

table artitecture

And the next step is your connection file for a connection with your database.

<?php

$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
 

<html>

<head>

<style type="text/css">

textarea{

border: 1px solid #ECCCCE;

padding: 2px;

}body{

color: #408800;

}

</style>

</head>

<body>

<?php

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 "<BR>";

echo "<a href='article.php'>View Your post artcile</a>";

}

else

{

echo "Error in your sql query please check again!";

}}

?>

<form name="form1" method="post" action="">

<div style="width:600px; margin:auto;">

<p><strong>Title:</strong></p><p><textarea name="title"  id="title" cols="40" rows="2"></textarea></p>

<p><strong>Description:</strong></p><p><textarea name="descr"  id="descr" cols="40" rows="2"></textarea></p>

<p><strong>Keyword:</strong></p><p><textarea name="keyword"  id="keyword" cols="40" rows="2"></textarea></p>

<p><strong>Category:</strong></p><p><select name="category">Please select category

                                                              <option value="php">php</option>

                                                              <option value="ASP">ASP</option>

                                                              <option value="JSP">JSP</option>

                                                              <option value="iPHONE">iPHONE</option>

                                    </select></p>

<p><strong>Content:</strong></p><p><textarea name="content"  id="content" cols="40" rows="10"></textarea></p>

<input type="submit" name="Submit" value="Post" style="color: #08322A; float: right; margin: 0px 254px;padding: 1px 11px;">

</div>

</form>

</body>

</html>


Output

submission form

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

Example

<html>

<head>

<style type="text/css">

textarea{

border: 1px solid #ECCCCE;

padding: 2px;

}body{

color: #69928E;

}strong{

color:#333333;

font-weight: normal;

}

</style>

</head>

<body>

<?php

include'connect.php';

$query="SELECT * FROM article";

$result=mysql_query($query);

?>

<?php

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

?>

<div style="width:600px; margin:auto;"><spam><a href="edit.php?id=<?php echo $rows['id']; ?>">Edit</a></spam>&nbsp;<spam><a href="delete.php?id=<?php echo $rows['id']; ?>">Delete</a></spam>&nbsp;<spam><a href="post.php">Post</a></spam></div>

<div style="width:600px; margin:auto;">

<p><strong>Title:</strong></p><p><?php echo $rows['title']; ?></p>

<p><strong>Description:</strong></p><p><?php echo $rows['descr']; ?></p>

<p><strong>Keyword:</strong></p><p><?php echo $rows['keyword']; ?></p>

<p><strong>Content:</strong></p><p><?php echo $rows['content']; ?></p>

</div>

<?php

}

?>

<?php

mysql_close();

?>

</body>

</html>


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

<html>

<head>

<style type="text/css">

textarea{

border: 1px solid #ECCCCE;

padding: 2px;

}body{

color: #408800;

}

</style>

</head>

<body>

<?php

include'connect.php';

$id=$_GET['id'];

$query="SELECT * FROM article WHERE id='$id'";

$result=mysql_query($query);

$rows=mysql_fetch_array($result);

?>

<form name="form1" method="post" action="update.php">

<div style="width:600px; margin:auto;">

<p><strong>Title:</strong></p><p><textarea name="title"  id="title" cols="40" rows="2"><?php echo $rows['title']; ?></textarea></p>

<p><strong>Description:</strong></p><p><textarea name="descr"  id="descr" cols="40" rows="2"><?php echo $rows['descr']; ?></textarea></p>

<p><strong>Keyword:</strong></p><p><textarea name="keyword"  id="keyword" cols="40" rows="2"><?php echo $rows['keyword']; ?></textarea></p>

<p><strong>Content:</strong></p><p><textarea name="content"  id="content" cols="40" rows="10"><?php echo $rows['content']; ?></textarea></p>

<p><strong>Type reason:</strong></p><p><input type="text" name="reason"  id="reason" value="<?php echo $rows['reason']; ?>"></p>

<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">

<input type="submit" name="Submit" value="Update" style="color: #08322A; float: right; margin: -44px 254px;padding: 1px 11px;">

</div>

</form>

<?php

mysql_close();

?>

</body>

</html>

 

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
 

<?php

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 "<BR>";

echo "<a href='article.php'>View Your updated data</a>";

}

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

 

<?php

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 "<BR>";

echo "<a href='article.php'>View Your updated data</a>";

}

else

{

echo "Error in your sql query please check again!";

}

?>
 

Output

delete article

successful delete massege

Up Next
    Ebook Download
    View all
    Learn
    View all