从mysql获取数据,如果结果===0,那么做些什么

ecbunoof  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(223)

我的主要任务是创建一个联系人列表。因此,用户按下按钮,然后用户在屏幕上看到,在电子邮件中键入的联系用户,然后如果从服务器的响应是“结果===0”的联系用户将成功地添加到联系人列表。如果来自服务器的响应是“result===-1”,则由于电子邮件不存在而找不到用户。
一切正常,除了一件事:即使电子邮件出错,也表明用户的响应是成功的。
我找不到我做错了什么。有人能帮我吗?
这是我的密码:
联系人列表(react native):

addNewContact = (email) => {
this.setState({
  promptVisible: false,
});
if (email === this.props.email) {
  Alert.alert('This is your username', 'You cannot add yourself to contacts!');
  return;
}
for (var i = 0; i < this.state.contacts.length; i++) {
  if (email === this.state.contacts[i].email) {
    Alert.alert('Existing Contact', 'Contact ' + email + ' already exists');
    return;
  }
}
this.showLoading();
fetch('LINK')
  .then((response) => response.json())
  .then((responseData) => {
    this.hideLoading();
    var result = responseData.result;
    var contactId = responseData.id;
    var name = responseData.name;
    if (result === 0) {
      console.log("Success");
      var newContacts = this.state.contacts.slice();
      newContacts[newContacts.length] = {
        contact_id: contactId,
        name: name,
        email: email
      }
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(newContacts),
        contacts: newContacts
      });
      Alert.alert('Contact added', 'User ' + email + ' was added to your contacts')
      console.log(newContacts);
    } else {
     if (result === -1) {
       Alert.alert('Not Found', 'User ' + email + ' not found');
       console.log("Bad");
       console.log(responseData);
     }
    }
  })
  .done;
}

服务器(php):

<?php
include 'DBConfig.php';

$conn = new mysqli($Name, $User, $Pass, $Database);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);
} 

$json = file_get_contents('php://input');

$obj = json_decode($json,true);

$email = $obj['email'];

$sql = "SELECT id, name, email FROM Registration WHERE email='$email'";

$result = mysqli_fetch_array(mysqli_query($conn,$sql));

if (isset($result)) {
  $result = array('result' => 0);
  echo json_encode($result);

} else {
  $result = array('result' => -1);
  echo json_encode($result);
}
 echo $json;
 $conn->close();
?>

upd:我发现在我的控制台上显示:

Success
 Array [
  Object {
   "contact_id": undefined,
   "email": "Djdf",
   "name": undefined,
 },
]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题