如何检查要插入mysql数据库的值

时间:2015-03-14 16:55:29

标签: php mysql

请问我是php和mysql的新手我想让你帮我解决如何检查插入数据库的值是否大于或小于,如果大于它应该插入传递,谢谢。

这个代码

$query="select * from ats_question";
$rs=mysql_query("select * from ats_question where test_id=$tid",$cn) or  die(mysql_error());
if(!isset($_SESSION[qn]))
{
$_SESSION[qn]=0;
mysql_query("delete from ats_useranswer where sess_id='" . session_id() ."'") or die(mysql_error());
$_SESSION[trueans]=0;

}
else
{   
        if($submit1=='Next Question' && isset($ans))
    {
            mysql_data_seek($rs,$_SESSION[qn]);
            $row= mysql_fetch_row($rs); 
mysql_query("insert into ats_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,
                true_ans,your_ans)values('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
            if($ans==$row[7])
            {
                        $_SESSION[trueans]=$_SESSION[trueans]+1;
            }
            $_SESSION[qn]=$_SESSION[qn]+1;
    }
    else if($submit=='Get Result' && isset($ans))
    {
            mysql_data_seek($rs,$_SESSION[qn]);
            $row= mysql_fetch_row($rs); 
mysql_query("insert into ats_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,
    your_ans) values 
('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
            if($ans==$row[7])
            {
                        $_SESSION[trueans]=$_SESSION[trueans]+1;
            }
            echo "<h1 class=head1> Result</h1>";
            $_SESSION[qn]=$_SESSION[qn]+1;
            echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
            echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
            $w=$_SESSION[qn]-$_SESSION[trueans];
            echo "<tr class=fans><td>Wrong Answer<td> ". $w;
            echo "</table>";
            mysql_query("insert into ats_result(login,test_id,grade,score) values
                ('$login',$tid,grade,$_SESSION[trueans])")
             or die(mysql_error());
            echo "<h1 align=center><a href=ats_review.php> Review Question</a> </h1>";
            unset($_SESSION[qn]);
            unset($_SESSION[sid]);
            unset($_SESSION[tid]);
            unset($_SESSION[trueans]);
            exit;
    }
 }
$rs=mysql_query("select * from ats_question where test_id=$tid",$cn) or      die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error  Occured</h1>";
session_destroy();
echo "Please <a href=index.php> Start Again</a>";

exit;
}
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
echo "<div align=\"center\" class=\"style2\"><strong> Ats Examination     </strong></div>";
echo "<div id=\"\"></div>";

    echo"<p id=\"note\"></p>";
echo "<form name=myfm id=myfm1 method=post action=ats_exam.php>";
echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";
$n=$_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ".  $n .": $row[2]</style>";
echo "<tr><td class=style6><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style6> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style6><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style6><input type=radio name=ans value=4>$row[6]";

if($_SESSION[qn]<mysql_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit1 value='Next Question'>   </form>";

echo "<tr><td><input type=submit name=submit value='Get Result'></form>";
echo "</table></table>";
?>

我想检查trueans是否大于15,如果等级值没有失败则应该插入通行证

 mysql_query("insert into ats_result(login,test_id,grade,score) values
            ('$login',$tid,grade,$_SESSION[trueans])")

1 个答案:

答案 0 :(得分:1)

谢谢我终于得到了我的问题的解决方案,我的代码现在看起来像mysql_query("insert into ats_result(login,test_id,score,grade)values('$login','$tid',$_SESSION[trueans],IF($_SESSION[trueans]>=10,'Pass','Fail')");感谢所有人的回应和挑战。