doushi1929 2015-03-04 15:37
浏览 32
已采纳

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

I want to check that if I have in my first drop down list the year is 2001 and in the second dropdown list i select 1900 as the year then I will echo a message to the user that he should select a year that the first is lower than the second (ex: 2001<2002) and if it has that error then it should store the data in my database until he change it.

My code to update the database is the below....

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

and i guess that my code in order to check the years is this ...

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

But I cant find where i should place it in order to work correctly

  • 写回答

2条回答 默认 最新

  • duanhuan3012 2015-03-04 15:42
    关注

    Something like this? It's entirely up to you though really. I am making the assumption that if you are showing the error you do NOT want to update the database

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!