使用一个字段值来确定另一个字段值

时间:2012-06-25 02:26:44

标签: php

我正在php中构建一个时间轴网站。时间线上的条目通过html输入表格输入到数据库中。

下面是我用来从html表单中获取输入值并将它们存储在数据库中的php:

// 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>";
    }

这对我的目的来说很好用,但我还是要输入一个世纪值。我希望php根据4位数年来确定世纪。

我想取4位year值并使用它来确定将存储在数据库中的century值。

例如,如果输入的年份是2002年,我需要'21'作为'世纪'数据库中的值。现在,我必须手动输入'21'(或者恰好是它的世纪),因为这个值对于内容在时间轴上的显示方式很重要(即$query = "SELECT * FROM fields WHERE century = '21' ORDER by year ASC";

最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

  

$ century = substr($ year,0,2)+ 1;

应该做的伎俩。也许添加一些验证来确保$ year是数字(也许它已经完成)。

相关问题