Creating an article database using PHP and MySQL.
Posted: Tue Jun 19, 2012 11:49 pm
Hi all,
First of all - I'm new to the forum and pretty new to PHP/MySQL. I have some experience but not a whole lot.
What I've been tasked with doing is this: create a database that is capable of creating, deleting, and editing entries and that has search functionality built in.
To complete this task I've put together several forms: add.php, edit.php, and search.php
Add.php and edit.php are essentially the same form, except that edit.php uses an ID to fill in the information that needs to be edited into a field, which the user can then edit and submit.
All of that functionality works great. Even the search functionality works for searching the fields. But I have been banging my head against the wall for over a week now trying to get the tag functionality to work.
Here is the situation:
An entry in the database consists of the following information - An article title, an article organization (or author), an access date, an article URL, and a series of "tags" that describe the article. The tags are presented to the user as a series of checkboxes that the user can check or uncheck.
So, for example, if an article titled Running 5 miles in 5 minutes then walking some by Winthrope T. Salinsurg resides at www.salinsurg.com/mycoolarticle.pdf and it were accessed today, the form would look like this:
Article Title: Running 5 miles in 5 minutes then walking some
Article Organization (or Author): Winthrope T. Salinsurg
Access Date: June 20, 2012
Article URL: www.salinsurg.com/mycoolarticle.pdf
Running [X]
Jumping [ ]
Climbing [ ]
Walking [X]
(here the X denotes that the checkbox was checked)
What I would like for my form to do is this:
send $articletitle, $articleorganization, $articledate, $articleurl to my MySQL table called articles (and as a matter of fact, the form does indeed do this properly already)
And then
look at the checkboxes and see which 2 have been checked, and take the names of those checkboxes (articletags_running, and articletags_walking) and look through my tags table to find their IDs an store them in variables.
Then, combine the new article ID (which was generated when the info was inserted into the table) with the tag IDs and insert those pairs into a third table called articles_tags
...It's really hard for me to explain this so I hope I'm explaining it correctly.
For reference my addnew.php code is pasted below:
First of all - I'm new to the forum and pretty new to PHP/MySQL. I have some experience but not a whole lot.
What I've been tasked with doing is this: create a database that is capable of creating, deleting, and editing entries and that has search functionality built in.
To complete this task I've put together several forms: add.php, edit.php, and search.php
Add.php and edit.php are essentially the same form, except that edit.php uses an ID to fill in the information that needs to be edited into a field, which the user can then edit and submit.
All of that functionality works great. Even the search functionality works for searching the fields. But I have been banging my head against the wall for over a week now trying to get the tag functionality to work.
Here is the situation:
An entry in the database consists of the following information - An article title, an article organization (or author), an access date, an article URL, and a series of "tags" that describe the article. The tags are presented to the user as a series of checkboxes that the user can check or uncheck.
So, for example, if an article titled Running 5 miles in 5 minutes then walking some by Winthrope T. Salinsurg resides at www.salinsurg.com/mycoolarticle.pdf and it were accessed today, the form would look like this:
Article Title: Running 5 miles in 5 minutes then walking some
Article Organization (or Author): Winthrope T. Salinsurg
Access Date: June 20, 2012
Article URL: www.salinsurg.com/mycoolarticle.pdf
Running [X]
Jumping [ ]
Climbing [ ]
Walking [X]
(here the X denotes that the checkbox was checked)
What I would like for my form to do is this:
send $articletitle, $articleorganization, $articledate, $articleurl to my MySQL table called articles (and as a matter of fact, the form does indeed do this properly already)
And then
look at the checkboxes and see which 2 have been checked, and take the names of those checkboxes (articletags_running, and articletags_walking) and look through my tags table to find their IDs an store them in variables.
Then, combine the new article ID (which was generated when the info was inserted into the table) with the tag IDs and insert those pairs into a third table called articles_tags
...It's really hard for me to explain this so I hope I'm explaining it correctly.
For reference my addnew.php code is pasted below:
Code: Select all
<?php
function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
. . .
</head>
<body>
<div class="container">
<div class="header">
. . .
</div>
<div class="sidebar1">
<ul class="nav">
. . .
</div>
<div class="content">
<div id="stylized" class="myform">
<form id="form" name="form" action="" method="post">
<h1>Create a new entry in the database</h1>
<table width="76%" border="0" cellpadding="6">
<tr>
<td colspan="2"><legend></legend></td>
</tr>
<tr>
<td width="20%" align="right"><span class="field">Article Title:</span></td>
<td width="80%" align="left"><span class="field">
<input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Article Author:</span></td>
<td align="left"><span class="field">
<input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Access Date:</span></td>
<td align="left"><span class="field">
<input name="articledate" type="text" value="MM/DD/YYYY" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Article URL:</span></td>
<td align="left"><span class="field">
<input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/>
</span></td>
</tr>
<tr>
<td align="right"><span class="field">Article Tags:</span></td>
<td align="left"><span class="field">
<input type="checkbox" name="articletags[]" value="geology" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" />
</span></td>
</tr>
<tr>
<td colspan="2" align="center" valign="middle"><input type="submit" name="submit" value="Add this Article" /></td>
</tr>
</table>
<footer></footer>
</form>
</div>
<div class="footer">
. . .
</div>
</body>
</html>
<?php
}
// connect to the database
include('settings.php');
if(count($articletags) > 0)
{
$articletags_string = implode(",", $articletags);
}
// check if the form has been submitted. If it has, start to process the form and save it to the database
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// get form data, making sure it is valid
$articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
$articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
$articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
$articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
$articletags = implode(',', $_POST['articletags']);
// check to make sure both fields are entered
if ($articletitle == '' || $articleorganization == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($articletitle, $articleorganization);
}
else
{
// save the data to the database
mysql_query("INSERT INTO articles SET articletitle='$articletitle',
articleorganization='$articleorganization',
articledate='$articledate',
articleurl='$articleurl',
articletags='$articletags' ")
or die(mysql_error());
// once saved, redirect to success page
header("Location:addsuccess.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','');
}
?>