Crime news portal source code in php
Here is a basic PHP code for a crime news portal:
Note: This is a simplified example and you may need to add more features, security measures, and error handling to make it a fully functional crime news portal.
Database Structure:
Create the following tables in your database:
news
:id
(primary key, auto-increment)title
description
category
(e.g. "murder", "theft", etc.)location
date
author
categories
:id
(primary key, auto-increment)name
(e.g. "murder", "theft", etc.)
comments
:id
(primary key, auto-increment)news_id
(foreign key referencing thenews
table)author
comment
date
PHP Code:
index.php (main page)
<?php
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Get latest news
$query = "SELECT * FROM news ORDER BY date DESC LIMIT 10";
$result = mysqli_query($conn, $query);
// Display news
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2>". $row['title']. "</h2>";
echo "<p>". $row['description']. "</p>";
echo "<p>Category: ". $row['category']. "</p>";
echo "<p>Location: ". $row['location']. "</p>";
echo "<p>Date: ". $row['date']. "</p>";
echo "<p>Author: ". $row['author']. "</p>";
echo "<br><br>";
}
// Close connection
mysqli_close($conn);
?>
news.php (individual news page)
<?php
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Get news by ID
$query = "SELECT * FROM news WHERE id = ". $_GET['id'];
$result = mysqli_query($conn, $query);
// Display news
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2>". $row['title']. "</h2>";
echo "<p>". $row['description']. "</p>";
echo "<p>Category: ". $row['category']. "</p>";
echo "<p>Location: ". $row['location']. "</p>";
echo "<p>Date: ". $row['date']. "</p>";
echo "<p>Author: ". $row['author']. "</p>";
echo "<br><br>";
// Display comments
$query = "SELECT * FROM comments WHERE news_id = ". $row['id'];
$result = mysqli_query($conn, $query);
while ($comment = mysqli_fetch_assoc($result)) {
echo "<p>". $comment['author']. ": ". $comment['comment']. "</p>";
}
}
// Close connection
mysqli_close($conn);
?>
add_news.php (add news form)
<?php
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Process form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = $_POST['title'];
$description = $_POST['description'];
$category = $_POST['category'];
$location = $_POST['location'];
$date = $_POST['date'];
$author = $_POST['author'];
// Insert news into database
$query = "INSERT INTO news (title, description, category, location, date, author) VALUES ('$title', '$description', '$category', '$location', '$date', '$author')";
mysqli_query($conn, $query);
// Redirect to news page
header('Location: index.php');
exit;
}
// Display form
echo "<form action='add_news.php' method='post'>";
echo "<label for='title'>Title:</label>";
echo "<input type='text' name='title'>";
echo "<br><br>";
echo "<label for='description'>Description:</label>";
echo "<textarea name='description'></textarea>";
echo "<br><br>";
echo "<label for='category'>Category:</label>";
echo "<select name='category'>";
echo "<option value='murder'>Murder</option>";
echo "<option value='theft'>Theft</option>";
// Add more categories here
echo "</select>";
echo "<br><br>";
echo "<label for='location'>Location:</label>";
echo "<input type='text' name='location'>";
echo "<br><br>";
echo "<label for='date'>Date:</label>";
echo "<input type='date' name='date'>";
echo "<br><br>";
echo "<label for='author'>Author:</label>";
echo "<input type='text' name='author'>";
echo "<br><br>";
echo "<input type='submit' value='Add News'>";
echo "</form>";
// Close connection
mysqli_close($conn);
?>
add_comment.php (add comment form)
<?php
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Process form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$news_id = $_POST['news_id'];
$author = $_POST['author'];
$comment = $_POST['comment'];
// Insert comment into database
$query = "INSERT INTO comments (news_id, author, comment) VALUES ('$news_id', '$author', '$comment')";
mysqli_query($conn, $query);
// Redirect to news page
header('Location: news.php?id='. $news_id);
exit;
}
// Display form
echo "<form action='add_comment.php' method='post'>";
echo "<label for='news_id'>News ID:</label>";
echo "<input type='hidden' name='news_id' value='". $_GET['id']. "'>";
echo "<br><br>";
echo "<label for='author'>Author:</label>";
echo "<input type='text' name='author'>";
echo "<br><br>";
echo "<label for='comment'>Comment:</label>";
echo "<textarea name='comment'></textarea>";
echo "<br><br>";
echo "<input type='submit' value='Add Comment'>";
echo "</form>";
// Close connection
mysqli_close($conn);
?>
This code provides basic functionality for a crime news portal, including:
- Displaying the latest news on the main page
- Displaying individual news pages with comments
- Allowing users to add news and comments
Note that this is a simplified example and you may need to add more features, security measures, and error handling to make it a fully functional crime news portal. Additionally, you should ensure that your database is properly secured and that you are using prepared statements to prevent SQL injection attacks.