Jump to content
xisto Community
Nqon

Creating A Content Managing System Using MySQL

Recommended Posts

This tutorial is for beginners that know some php and know the basics of MySQL. I will tell you how to make a simple Content Managing System. A Content Managing System is a way of editing and adding your content using your browser, so you dont have to go trough ftp all the time.

 

This CMS uses MySQL databases. So what we need to do is to create a database. Well go through this step by step to make it as easy as posible:

 

1. Go in to your cPanel and select phpMyAdmin.

 

2. Write the name of your database in the input box under the text: Create new database. This database should be named like this "yourusername_name". So mine would be nqon_cms. With Xisto you must have your username first (at least if your being hosted as a subdomain). We will be calling the database "your_database" in this tutorial.

 

Now we need to create a table where all the content should be. It is in this table we add and edit the content.

 

1. Im assuming your still inside phpMyAdmin, at your left you should now have the name of your database. Click it.

 

2. You should now get up a text that says: Create new table on database your_database. There you should fill in the name, in this tutorial we'll call it "content". It should have 4 fields

 

3. The first should look like this(if somethings not specified leave it as it is):

First: Field = id | Type = INT | Length/Values = 2 | Extra = auto_increment

Second: Field = page | Type = varchar | Length/Values = 20 |

Third: Field = tittle | Type = varchar | Length/Values = 30 |

Fourth: Field = content | Type = longtext

 

4. Then press Go or Submit or whatever it says.

 

The first thing that should be done is to connect to the database, this should be in the top of every page (you can use a include() for it if you like, if you dont know what im talking about its not a problem). We'll do it this way:

 

connect to database code

<?php/*Change this to your settings*/  $db       = "your_database";  $db_user[tab][/tab]= "yourusername";  $db_pass  = "yourpassword";$db_connect = mysql_connect("localhost", $db_user, $db_pass)or die("Could not connect to database.");mysql_select_db($db, $db_connect);?>
You should change your_database to your database name. In my case it would be $db = "nqon_cmd". Username and password should be the same as your cPanel log inn password and username.

 

Now thats done. Its time to make a page so you can add content to the content table you created. We'll call this file addcontent.php.

 

addcontent.php

<html><head><title>Insert Content</title></head><body><?phpif(isset($_POST['insertcontent'])){$page   = $_POST['page'];$tittle   = $_POST['tittle'];$content   = $_POST['content'];[tab][/tab]if(!$page OR !$tittle OR !$content){  echo "Go back and fill out all fields";  }[tab][/tab]else{  $insertcontent = "   INSERT INTO content   (page, tittle, content)  VALUES   ('$page', '$tittle', '$content')  ";  mysql_query($insertcontent) or die("<p><b>A fatal MySQL error occured</b>.\n<br />Query: " .   $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());  echo "Your new content page was added!";[tab][/tab]};}else { echo ?><form method="post"><table border="0"><tr><td width="100"><b>page:</b></td><td><input type="text" name="page"  size="30" maxlength="20" class="textfield" /></td></tr><tr><td><b>Tittle:</b></td><td><input type="text" name="tittle" size="30" maxlength="30" class="textfield" /></td></tr><tr><td></table><p><b>Content:</b><br><textarea name="content" rows="14" cols="60"></textarea><p><input name="insertcontent" type="submit" value="Add Content!"/></center></form></td></tr><table><?php; };?></body></html>
Make sure that you place the "connect to database code" at the very top of this page. Its important that your main page has the page value "main". The page value is how we call the content, you should not have 2 pages with the same page value. Your page value should not have spaces. The tittle value will appear in the <tittle> </tittle> tags.

 

Now we would like to see that content which we just added. So its time to create the index.php page.

 

index.php

<?phpif(!isset($page)) {$page="main"; };$db_query_page = mysql_query("SELECT * FROM content WHERE page='$page' ");$data_page = mysql_fetch_array($db_query_page);$db_query_showlinks = mysql_query("SELECT * FROM content ORDER BY id ASC");?><html><head><title><?php echo $data_page["tittle"]; ?></title></head><body><table><tr><td valign="top"><?phpwhile($data_showlinks = mysql_fetch_array($db_query_showlinks)){  echo "<a href='/?page=";  echo $data_showlinks['page'];  echo "'>Âť ";  echo $data_showlinks['page'];  echo "</a>";};?></td><td valign="top"><?php echo $data_s["innhold"]; ?></td></tr></table></body></html>
Here as well you need to insert the connect to database code at the top.

 

Now, you might want to edit the content every once in a while, so its time to work on that file. We'll call that file editcontent.php.

 

editcontent.php

<html><head><title>Edit Content</title></head><body><?phpif(isset($_POST['editcontent'])){$page   = $_POST['page'];$tittle   = $_POST['tittle'];$content   = $_POST['content'];[tab][/tab]if(!$page OR !$tittle OR !$content){  echo "Go back and fill out all fields";  }[tab][/tab]else{  $editcontent = "   UPDATE content SET page='$page' tittle='$tittle' content='$content' WHERE id='$id' ";  mysql_query($editcontent) or die("<p><b>A fatal MySQL error occured</b>.\n<br />Query: " .   $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());  echo "The page was succesfully edited!";[tab][/tab]};}elseif(isset($_POST['edit'])) { $id= $_POST['edit'];$db_query_edit = mysql_query("SELECT * FROM content WHERE id='$id' ");$data_edit = mysql_fetch_array($db_query_edit);echo ?><form method="post" action="editcontent.php?id=<?php echo $id; ?>"><table border="0"><tr><td width="100"><b>page:</b></td><td><input type="text" name="page" value="<?php echo $data_edit['page']; ?>" size="30" maxlength="20" class="textfield" /></td></tr><tr><td><b>Tittle:</b></td><td><input type="text" name="tittle" value="<?php echo $data_edit['tittle']; ?>"size="30" maxlength="30" class="textfield" /></td></tr><tr><td></table><p><b>Content:</b><br><textarea name="content" rows="14" cols="60"><?php echo $data_edit['content']; ?></textarea><p><center><input name="editcontent" type="submit" value="Add Content!"/></center></form></td></tr><table><?php; } else{$db_query_showlinks = mysql_query("SELECT * FROM innhold ORDER BY id ASC");?><form action="" method="post"><select name="edit"><?phpwhile($data_showlinks = mysql_fetch_array($db_query_showlinks)){  echo "<option value='";  echo $data_showlinks['id'];  echo "'>";  echo $data_showlinks['page'];  echo "</option>";};?></select><input type="submit" name="contentedit" value="Edit this page!" /></form><?php }; ?></body></html>

Now Im tired of writing. This should work well, but it might need some final editing first. Im going to make a version of this at my homepage for your viewing pleasure.

 

Its most likely a bit bugged at the moment, but Im going to fix it soon, I just need to rest a bit first. Any feedback is welcome.

Share this post


Link to post
Share on other sites

Im going through the tutorial today, is it posible for me to get some extra points for this. I mean, I wrote all those things in the code and you dont get points for that... :D

Share this post


Link to post
Share on other sites

Thats right... you dont get any points in code or quote tags. Its okey, else people quoting each other would get points, and so would people that copy some code of a page.

Share this post


Link to post
Share on other sites

Nqon, i know how to do what you explained. But there's is something i don know yet... and i would to to learn... how to create users and passwords????? do u know how?

Share this post


Link to post
Share on other sites

What is it you cant do with users and passwords? If you know how to do what I explained it shouldnt be any problems beside the cookie/session problem.If you know how to add a new row to a table, and how to edit it it should not be any problem to add a user and let him change his profil. I made an excellent userdatabase on my site that allows me to have admin features, and several different userlevels, so only some people can visit sensitive pages (nothing illigal, just tactics for my clan :D ).Anyway, if your problem is creating a userdatabase you obviously dont know what I tryed to explain here, but if your problem is to use cookies I could write a guide about it. Its not really that hard.

Share this post


Link to post
Share on other sites

Im going through the tutorial today, is it posible for me to get some extra points for this. I mean, I wrote all those things in the code and you dont get points for that... :P


Dude not everyone knows how to read PHP well. Really you should explain every line. A tutorial is something that explaines how something works, not just a bunch of code. I can read it but can average Bob read that. I know i can explain everything, buts its not my tutorial. If you wanna get more points from the admins, then maybe explain your tutorial, bit by bit.

Share this post


Link to post
Share on other sites

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
What is wrong?

Yeah, don't do that again. When someone has an answer for you, they will reply. Don't post the same one line message over and over again. To begin with, the error message already tells you what to do! Assuming that you only have one field set for auto-increment, then go and set that as the table key!

If you have more than one field set for auto-increment, then unset the one that isn't correct.

Next time you want help with something, try to include some information with your request like an actual request.

vujsa

Share this post


Link to post
Share on other sites

Well I have to say, thank you for taking the time to write this tutorial. For somebody who knows php fairly well, it is easy to follow.However, this doesn't seem like a very secure way to go about content management, as any old person could come along and edit or add pages.Also, it might be a little more helpful if you went into further detail with your explanations of the code....it is mainly code and little explanation, that means if I don't know much in the way of php or how to connect to a mysql database already....the code may not be very helpful to me.

Share this post


Link to post
Share on other sites

However, this doesn't seem like a very secure way to go about content management, as any old person could come along and edit or add pages.

And not only this. While the code is all fine, it is not secure enough. For example, if a host didn't have Magic Quotes on, inserting a text with apostrophes with in it would cause a MySQL error to occur. So, even if you are writing simple code (like this), it is highly recommended to sanitize user-entered data.

Share this post


Link to post
Share on other sites

Below I have an attached file and under the text create new database it says: No Privileges. What exatly does this mean? I am confused. I barely know the slightest amount of php (i'm not that good of a scriptor i'm not stupid either lol). Help?

Share this post


Link to post
Share on other sites

Below I have an attached file and under the text create new database it says: No Privileges. What exatly does this mean? I am confused. I barely know the slightest amount of php (i'm not that good of a scriptor i'm not stupid either lol). Help?

You cannot Create or Delete the databases from within MYSQL because the default user does not have the appropriate privileges. Instead, use the Web Hosting Control Panel (Site Management Tools -> MYSQL Databases) to Create or Delete Databases. Edited by turbopowerdmaxsteel (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.