0

各位程序员大家好。如果输入的值低于 db 中的值,我想在我的数据库中禁用表更新。代码看起来不错,但更新总是执行。这是我的代码:

$sql="UPDATE student SET _id_year='$_year' WHERE _index='$_index'";
$check1="SELECT _id_year FROM student WHERE _index='$_index'";

if('$_year'>'$check1')
{
mysqli_query($con,$sql);
}

注意:_id_year 和 _index 是来自 DB 的值,而$_year,$_index是输入值。 $con是连接到数据库。

4

4 回答 4

2

首先执行您的选择查询并获取$check1. 然后比较。

$qry    =  "SELECT _id_year FROM student WHERE _index='$_index'";
$exec   =  mysqli_query($con,$qry);
$result =  mysqli_fetch_object($result);
$check1 =  $result->_id_year ;

另外,您没有使用单引号。试试这个,

if($_year > $check1)
{
  mysqli_query($con,$sql);
}
于 2013-04-10T15:21:11.767 回答
1

使用SafeMysql,代码与您编写的代码几乎相同,但它实际上会运行这些查询并使其安全

$check=$db->getOne("SELECT _id_year FROM student WHERE _index=?s",$_index);

if($_year > $check )
{
    $db->query("UPDATE student SET _id_year=?s WHERE _index=?s",$_year,$_index);
}
于 2013-04-10T15:27:53.887 回答
0

您应该先执行$check1查询,获取结果,然后将其与您的$_year变量进行比较

于 2013-04-10T15:22:39.780 回答
-1

这总是正确的,因为你的 PHP 的引用字段不应该被引用

 if($_year>$check1)
 {
 mysqli_query($con,$sql);
 }
于 2013-04-10T15:22:56.463 回答