I’m building a timeline site in php. The entries on the timeline are inputed into the database via html input forms.
Below is the php that I’m using to take the inputed values from the html forms and store them in the database:
// Collects information from the forms //
$year = mysql_prep($_POST['year']);
$concept = mysql_prep($_POST['concept']);
$author = mysql_prep($_POST['author']);
$text = mysql_prep($_POST['text']);
$century = mysql_prep($_POST['century']);
$file_under = mysql_prep($_POST['file_under']);
$pic=($_FILES['photo']['name']);
$query = "INSERT INTO fields (year, concept, author, text, century,
file_under, photo, created, modified)
VALUES ('{$year}', '{$concept}', '{$author}', '{$text}', '{$century}',
'{$file_under}', '{$pic}', NOW(), NOW())";
$result = mysql_query($query, $connection);
if ($result) {
redirect_to("index.php");
} else {
//display error message
echo "<p>Yikes!</p>";
echo "<p>" . mysql_error() . "</p>";
}
This works fine for my purposes now, but I still have to input a century value. I want php to determine the century based on the 4 digit year.
I want to take the 4-digit year value and use this to determine the century value that will be stored in the database.
So for example, if the year entered is 2002, I need ’21’ as the value to be put in the database for ‘century’. Right now, I have to manually enter ’21’ (or whatever century it happens to be), because this value is important for how the content gets displays on the timeline (i.e., $query = "SELECT * FROM fields WHERE century = '21' ORDER by year ASC";)
What’s the best way to do this?
Should do the trick. Maybe add some validation to make sure $year is numeric (maybe it’s already done).