从两个下拉列表中比较两个不同的年份,如果第一年高于第二年,则回复一条消息


Compare two different years from two dropdown lists and if the first year is higher than the second then echo a message

我想检查一下,如果我的第一个下拉列表中的年份是2001,并且在第二个下拉列表中将1900选择为年份,那么我会向用户回显一条消息,告诉他应该选择第一个低于第二个的年份(例如:2001<2002),如果出现错误,那么它应该将数据存储在我的数据库中,直到他更改为止。

我更新数据库的代码如下。。。。

if(isset($_POST['id'])){
        $id = $_POST['id'];
        $school = mysql_real_escape_string($_POST["school"]);
        $degree = mysql_real_escape_string($_POST["degree"]);
        $website = mysql_real_escape_string($_POST["website"]);
        $start_date = mysql_real_escape_string($_POST["start_date"]);
        $end_date = mysql_real_escape_string($_POST["end_date"]);
        $start_year = mysql_real_escape_string($_POST["start_year"]);
        $end_year = mysql_real_escape_string($_POST["end_year"]);
        $degree_description = mysql_real_escape_string($_POST["degree_description"]);
        $query="UPDATE education
                SET school = '$school', degree = '$degree', website = '$website', start_date='$start_date', end_date='$end_date', start_year='$start_year', end_year='$end_year', degree_description='$degree_description'
                WHERE id='$id' AND username='$username'";
        mysql_query($query)or die(mysql_error());
        if(mysql_affected_rows()>=0){
            echo "<p>($username) Record Updated<p>";
        }else{
            echo "<p>($username) Not Updated<p>";
        }
    }
    else{
      //first time, initialize as you wish. Probably need to get the first id for this user, using another query
      $id = 0;
    }

我想我的代码是为了检查年份。。。

if($start_year>$end_year)
{
    echo "The error Message";
}

但我找不到我应该把它放在哪里才能正确工作

类似的东西?这完全取决于你。我假设,如果你显示错误,你不想更新数据库

if(isset($_POST['id'])){
    $id = $_POST['id'];
    $school = mysql_real_escape_string($_POST["school"]);
    $degree = mysql_real_escape_string($_POST["degree"]);
    $website = mysql_real_escape_string($_POST["website"]);
    $start_date = mysql_real_escape_string($_POST["start_date"]);
    $end_date = mysql_real_escape_string($_POST["end_date"]);
    $start_year = mysql_real_escape_string($_POST["start_year"]);
    $end_year = mysql_real_escape_string($_POST["end_year"]);
    $degree_description = mysql_real_escape_string($_POST["degree_description"]);
    if($start_year > $end_year){
        echo 'The error Message';
        $good = false;
    }else{
        $good = true;
    }
    if($good == true){
        $query="UPDATE education
                SET school = '$school', degree = '$degree', website = '$website', start_date='$start_date', end_date='$end_date', start_year='$start_year', end_year='$end_year', degree_description='$degree_description'
                WHERE id='$id' AND username='$username'";

        mysql_query($query)or die(mysql_error());
        if(mysql_affected_rows()>=0){
            echo "<p>($username) Record Updated<p>";
        }else{
            echo "<p>($username) Not Updated<p>";
        }
    }   
}
else
{
    //first time, initialize as you wish. Probably need to get the first id for this user, using another query
    $id = 0;
}

在检查数据是否提交时,应检查年份。

if (isset($_POST['id'])) {
// All your data
if ($start_year > $end_year) {
// Echo the error message
} else {
// If the years are correct, store the data in the database
}
}

希望这能有所帮助。