Webdesign chapter ten CMS Part 2

So basically we now have a table which could hold content if we wanted it to. Obviously that doesn’t get us all that far. Now we have the hard parts, first we need to get data into said table, then we need to get it back out.

I’m going to use php to do all of the data stuff, you could obviously also use ASP (which makes things very easy, considering the visual studio is free and an excellent tool, not just for ASP development (can write neat little programs with it pretty quickly, too), or you could use ruby, which has a nice framework for web development with ruby on rails, or even a number of other languages. I use php because it’s free, fast, and supported by damn near any web server out there.

So for now we’re going to make a page that has nothing but a form on it, a very simple one at that, with only a few text inputs, where one could just enter all the data we need to make a whole new website.

First we need to connect to the database (the following code is all php code, so i’m omitting the php tags):

$connection = mysql_pconnect(‘localhost’, ‘xyious’, ‘password’);

with that we’re making a new variable called $connection, to which we’re saving the result of the function mysql_pconnect. That function makes a persistent connection to the database, which I use in favor of mysql_connect for several reasons. mysql_connect should be used in most cases, though. The parameters for that function are server, user, password. Now we need to select the Database we’re using:

mysql_select_db(“WebsiteDB”, $connection) or die(mysql_error());

That function obviously selects our db, if no connection can be made to the database, the whole script will just stop functioning right there and then, and give out an error (that’s what that or die is for, which calls the function mysql_error() which gets the error that occurred.

Now we’re going to evaluate the input we get from our form (yes this seems weird, since we don’t even have that form yet…. but we’re getting there in a minute).

We’re going to make a variable named error that we increment every time there is an error, and then we check the variable after validating all the input to see if everything worked correctly:

$error = 0;
if (!$_POST[‘Author’]) $error++;

and we check URL, Headline and Content the same way. We don’t check tags or description because those don’t need to have a value. Next we’re going to build our query string, which will insert the values we get into our database:

if (!$error) {
$query = “INSERT INTO xyious_content (Author, URL, Headline, Content”;
if ($_POST[‘Tags’]) { $query .= “, Tags”;}
if ($_POST[‘Description’]) { $query .= “, Description”;}
$query .= “) VALUES (‘” . $_POST[‘Author’] . “‘, ‘” . $_POST[‘URL’] . “‘, ‘” . $_POST[‘Headline’] . “‘, ‘” . $_POST[‘Content’];
if ($_POST[‘Tags’]) { $query .= “‘, ‘” . $_POST[‘Tags’];}
if ($_POST[‘Description’]) { $query .= “‘, ‘” . $_POST[‘Description’];}
$query .= “‘)”;

That is quite a bit of code for someone just starting, but mainly we’re just making a new variable named query that holds the query string, and then just appends strings to the end of it, like tags if we entered any. The syntax (we use) to put our data into our database is:

INSERT INTO <tablename> (<column1>, <column2>, …) VALUES (“<value1>”, <value2>, …)

The columns do not have to be in the same order as they are in the database, but you don’t need to specify which columns you’re putting which values into if you put a value into every column you have. I need to specify the columns because I’m leaving out 2 columns (ID and Timestamp) which are automatically filled if no value is provided.

Now we just need to execute the query and our content will be in the database:

if (mysql_query($query, $connection)) {
echo “<h2>Content Added</h2>”;
} else {
echo “<h2>” . mysql_error() . “</h2>”;
}

We’re just giving out a short message if content was added successfully, and the error if it wasn’t.

Next we just display the form so we can actually get some data and then we’re done:

<form action='<?php echo $PHP_SELF;?>’ method=’post’>
<table frame=”void” rules=”none”>
<tr><td><label for=”Author”>Author</label></td>
<td><input type=”text” id=”Author” name=”Author” size=”100″></td></tr>

This is mainly just html. <form> will make a new form, action tells the web page what to do with the data it gets, in this case we’re just putting a php variable there that will put the address of itself (hence the evaluation code on top there, the form and the evaluation are the same page, it doesn’t have to be and in many cases you have one page for the form and another page that just does the evaluation, but it’s fairly straightforward to have it all together in one page, especially if it doesn’t get outrageously long), method is the method of submitting the data, get and post are the 2 options we could use. get doesn’t allow for values over 255 characters, so we have to use post here. Then we make a table just to align things right, and then we have the inputs. The label just puts a label on the input so we know what we have to enter, and then we just have input type text so we have a text field that we can enter data into. We do the same (label and input) for url, tags, headline, description. We use a textarea for the content, since it potentially gets very long, and then we just have a submit button that sends our values to the database:

<td><textarea id=”Content” name=”Content” Cols=”100″ rows=”25″></textarea></td></tr>
<tr><td></td><td><input type=”submit” name=”submit” value=”add”></td></tr>

And that’s it. Now we can put stuff into our database, with no way of getting it back out. It’s also not very comfortable.

Leave a Reply