Lets say you have page articles.php in admin panel where you have listed all articles from database.
This is only a basic example and its not escaped for mysqli injection so "DON'T USE THIS ON LIVE SERVER", this is only that you get perception how things works, maybe is not a best way but its good to learn.
articles.php
<?php
error_reporting(1);
// mysqli connection
$conn = mysqli_connect('host', 'user', 'password', 'database name');
// check for connection
if (mysqli_connect_errno())
{
die("Connection failed : " . mysqli_connect_error());
exit();
}
// action to EDIT article
if (isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id']))
{
$id = (int)$_GET['id'];
// here is data for a single article fetched over id
$query = mysqli_query($conn, "SELECT * FROM articles WHERE id = '$id'");
// check if query runs
if ($query)
{
$data = mysqli_fetch_assoc($query);
// make UPDATE when u process form to update data in database
// if submit button is pressed
if (isset($_POST['submit']))
{
// get data from form
$title = $_POST['title'];
$text = $_POST['text'];
// update query
$update = mysqli_query($conn, "UPDATE articles SET title = '$title', text = '$text' WHERE id = '".$data['id']."'");
// check if update is successfuly
if ($update)
{
// if updated redirect to articles.php
header("Location: articles.php");
exit();
}
else
{
echo "Mysqli error : " . mysqli_error($conn);
}
}
echo '<form action="" method="post">
<label for="title">Title</label>
<input type="text" id="title" name="title" value="'.$data['title'].'"><br />
<label for="text">Text</label>
<textarea id="text" name="text">'.$data['text'].'</textarea><br />
<input type="submit" name="submit" value="Submit">';
}
else
{
echo "Mysqli error : " . mysqli_error($conn);
}
}
// action for DELETE article
else if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id']))
{
// delete article where id from url is same as id in database
$id = (int)$_GET['id'];
$query = mysqli_query($conn, "DELETE FROM articles WHERE id = '$id'");
// check if query runs
if ($query)
{
// if query was successfuly get back on articles list
header("Location : articles.php");
exit();
}
else
{
die("Mysqli error : " . mysqli_error($conn));
exit();
}
}
// if no action display all articles
else
{
// query to get all articles
$query = mysqli_query($conn, "SELECT * FROM articles");
// check if query runs
if ($query)
{
// get all articles in array
$article = mysqli_fetct_array($query);
echo '<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Posted date</th>
<th>Actions</th>
</tr>';
// loop through arcticles
while ($article)
{
echo '<tr>
<td>'.$article['id'].'</td>
<td>'.$article['title'].'</td>
<td>'.$article['date'].'</td>
<td><a href="articles.php?id='.$article['id'].'&action=edit">Edit</a> | <a href="articles.php?id='.$article['id'].'&action=delete">Delete</a></td>
</tr>';
}
echo '</table>';
}
else
{
echo "Mysqli error : " . mysqli_error($conn);
}
}
?>
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.