插入返回True但未插入数据

时间:2012-10-02 01:50:38

标签: php sql mysqli

我有一个函数,首先检查是否存在行,如果不存在则插入数据。如果有任何失败,它应该返回false。当我运行该函数并使用var_dump检查返回时,它返回true。我假设数据已插入数据库,但事实并非如此。它为什么要这样做?我的功能有问题吗?

function add_tier ($mysqli, $project_id, $tier_number, $wanted, $in_return, $limit) {

    if ($result = $mysqli->query("SELECT COUNT(tier_id) FROM `tier` WHERE `project_id` 
    = $project_id AND `tier_number` = $tier_number")){

      if ($return < 1) {

        if ($stmt = $mysqli->prepare("INSERT INTO `tier` (`project_id`, 
        `tier_number`, `wanted`, `in_return`, `limit`) VALUES (?, ?, ?, ?, ?)")){

        $stmt->bind_param('iissi', $project_id, $tier_number, $wanted, $in_return, 
        $limit);

        $return = $stmt->execute();

        $stmt->close();

        return $return;

        } else {return false;}      

      } else {return false;}            

    }  else {return false;}

}

1 个答案:

答案 0 :(得分:1)

(继续评论)您似乎没有在任何地方定义此行中的$return

if ($return < 1) {