为什么if if语句返回false?

时间:2018-05-11 21:24:37

标签: php mysqli

我面临的问题是我输入的条件句返回false!

class App extends Component {
  constructor(props){
    super(props);
    this.state = {
      elements: [],
      dataLoaded: false,
    }
  }
  loadData = () => {
    fetch('http://localhost:3001/')
    .then(res => res.json())
    .then(data => {this.setState({elements: data, dataLoaded: true })})
    .catch(err => console.log(err))
  }
    newElement = () => {
        this.setState({dataLoaded: false}, () => {
            this.loadData();
        })
    }

  componentDidMount(){
    this.loadData();
    console.log(this.state.feladatok)

  }
  render() {
    return (
      dataLoaded ? <div className="App">
        <Element newElem={() => this.newElement()}/>
      </div> : <div>Loading......</div>
    );
  }
}

我需要说if ($db->addkey($key_value,$key_balance,$key_created_date)) { echo 'TRUE'; } else { echo 'FALSE'; } 工作并将值传递给数据库是完美的,但我仍然知道为什么这个语句返回false?

$db->addkey($key_value,$key_balance,$key_created_date功能是:

addkey

变量是:

    public function addkey($key_value,$key_balance,$key_created_date){
    if ( !$this->link ) return false;
    $query = "INSERT INTO `" . $this->table_prefix . "keys` (`key_value`,`key_balance`,
        `key_created_date`)
        VALUES('" . $this->realEscapeSingle($key_value) .
        "', '" . $this->realEscapeSingle((int)$key_balance) . "', '" . $this->realEscapeSingle($key_created_date) . "')";

    ++$this->queries_;
    mysqli_query($this->link,$query);

    $key_id = mysqli_insert_id($this->link);
    if( !$key_id )
    {
        echo mysqli_errno($this->link) . ": " . mysqli_error($this->link);
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

您需要在return true方法中的某处添加->addkey。如果没有检测到错误,可能在底部?

答案 1 :(得分:1)

您的问题是addkey方法永远无法返回 true

因此,当您在if(...)语句中测试方法返回时,无论方法是否执行其预期任务,if都将永远不会运行。

参见代码:

public function addkey($key_value,$key_balance,$key_created_date){
    ....

  $key_id = mysqli_insert_id($this->link);
  if( !$key_id )
   {
     echo mysqli_errno($this->link) . ": " . mysqli_error($this->link);
     return false;
   }

 return true; // this catch that if none of the return false 
              // are triggered then the function MUST be true. 

}