MySQLI和Select 1

时间:2014-02-23 23:51:57

标签: php mysql

从数据库获取数据时,我的if语句没有按预期工作,即使我在该数据库中为该用户设置了$ badgename,我也得到了“你得到一个新徽章”。但它没有把它穿上。

我是MySQLI的新手,所以这可能是我错过的......

$numberofposts=$row['posts'];
$userid = $_SESSION['userid'];
$badgename = "Legend";

if($numberofposts >= 10){

$SQL = $mysqli->query("SELECT 1 FROM `badges` WHERE `mid`='$userid' AND 'badge' = '$badgename'");
$num = $SQL->num_rows;

if($num > 0){
 echo "You got a new badge";
 $mysqli->query("INSERT INTO badges ('mid', 'badge') VALUES ('$userid', '$badgename')");
}
else 
{
echo "You already have this badge";
}

感谢。

2 个答案:

答案 0 :(得分:3)

你在两个问题中都有拼写错误:

用反引号(`)替换badge上的单引号(') 来自您的选择查询。

然后在插入查询中用反引号替换单引号。例如:

 $numberofposts=$row['posts'];
 $userid = $_SESSION['userid'];
 $badgename = "Legend";

if($numberofposts >= 10){

 $SQL = $mysqli->query("SELECT 1 FROM `badges` WHERE `mid`='$userid' AND `badge` =     '$badgename'");
 $num = $SQL->num_rows;

 if($num > 0){
  echo "You got a new badge";
 $mysqli->query("INSERT INTO `badges` (`mid`, `badge`) VALUES ('$userid', '$badgename')");
 }
 else 
{
echo "You already have this badge";
}

答案 1 :(得分:2)

您的INSERT查询错误。

您的列名称周围有单引号,但您应该使用反引号(例如在SELECT查询中)