Simple-Ass Content Management System

Hello!

I recently built a personal project,

A simple content management system and thought I would share it with you guys!

This example is run on local host, built in php and myphpAdmin.

1. Html

<!DOCTYPE html>
<html>
<head>
		<link rel=stylesheet type ="text/css" href="style.css">
<title>Main</title>
</head>
<body>
<h1> CMS</h1>
<form action="insert.php" method="post" enctype="multipart/form-data">
<div id="headerBox">
<input type="text" name="subject" id="subject"></div>
<div id="commentBox">
<input type="text" name="textBox" id="textBox"></div>
<input type="file" name="image" />
<button>Upload</button>
<div id=button>
<input type="submit" name="submit" id="sendButton" value="send"></div>
</form>

</body>
</html>

The first step is pretty basic, this is mainly setting up the html “skeleton”. When a form is submitted the page is refreshed, the enctype is a multipart from that allows images to be uploaded. There is then some simple div header tags; inside of these are input types, text and file (for the image upload). Finally a submit button.

2.CSS

body {
width: 100%;
height: 100%;
margin: auto;
text-align: center;
}
#commentBox{
text-align: center;
}
#textBox{
height: 200px;
width: 400px;
margin: auto;

}

#button{
text-align: center;

}
#headerBox{
text-align: center;
}

h1{
text-align: center;

}

The CSS is fairly simple  and self explanatory the idea is that the form is centered.

 

3. php admin setup

Create a database layout similar to this.

2017-03-13 (1)

The ID acts as a primary key, Then there is Title and Article which are both varchar data-types. Finally the image is a blob type.

 

4.PHP

<?php
$con=mysqli_connect('localhost','root','password','database');

if(mysqli_connect_errno()){
echo "failed to connect: " .mysqli_connect_error();

}
$subject = mysqli_real_escape_string($con,$_POST['subject']);
$text = mysqli_real_escape_string($con,$_POST['textBox']);

if(isset($_FILES['image'])){
$image = $_FILES['image']['tmp_name'];
}

$img = file_get_contents($image);

// create a variable
$subj=$_POST['subject'];
$artc=$_POST['textBox'];

//Execute the query

mysqli_query($con,"INSERT INTO default (Title,Article,Image)
VALUES ('$subj','$artc','$image')");

if(mysqli_affected_rows($con) &gt; 0){
echo "good";
} else {
echo "problem
";
echo mysqli_error ($con);
}

So firstly a database connection is made with the credentials of “root” and “password”. After this there is an error handler if the connection isn’t made. Next the image is collected from the file upload.  Article and subject  types are posted.  Lastly the query is fired updating the system, there is an error handler to give feedback if something went wrong.

And That’s it!

Hopefully this helps someone somewhere! Happy coding 🙂

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s