0

I'm trying to insert data into my MySQL database but I'm getting an error that says "Network request failed" on my simulator. I'm guessing fetch()'s it has something to do with my systems IP address (even though I entered the right one).

The App.js file is my React Native code. The DBConfig.php file identifies my database info. The submit_user_info.php file is responsible for populating the database and the config.inc.php (not included in this post because it's too long) file has my database configurations.

Note: config.inc.php is found in MAMP > bin > phpMyAdmin > config.inc.php

Note: In DBConfig.php I left $HostUser and $HostPass blank (='') because inside config.inc.php, on lines 568-569, both $cfg['SQLValidator']['username'] and $cfg['SQLValidator']['password'] are left blank.

Error inside iOS simulator

Here's App.js:

import React, { Component } from 'react';

import { AppRegistry, StyleSheet, TextInput, View, Alert, Button } 
from 'react-native';

class ThisApp extends Component {

constructor(props) {

    super(props)

    this.state = {

      TextInputName: '',
      TextInputEmail: '',
      TextInputPhoneNumber: ''

    }

  }

  InsertDataToServer = () =>{


 const { TextInputName }  = this.state ;
 const { TextInputEmail }  = this.state ;
 const { TextInputPhoneNumber }  = this.state ;


fetch('https://MySystemsIPAddress/submit_user_info.php', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({

    name: TextInputName,

    email: TextInputEmail,

    phone_number: TextInputPhoneNumber

  })

}).then((response) => response.json())
      .then((responseJson) => {

// Showing response message coming from server after inserting records.
        Alert.alert(responseJson);

      }).catch((error) => {
        console.error(error);
      });


  }

  render() {
    return (

<View style={styles.MainContainer}>

        <TextInput

          // Adding hint in Text Input using Place holder.
          placeholder="Enter Name"

          onChangeText={TextInputName => this.setState({TextInputName})}

          // Making the Under line Transparent.
          underlineColorAndroid='transparent'

          style={styles.TextInputStyleClass}
        />

        <TextInput

          // Adding hint in Text Input using Place holder.
          placeholder="Enter Email"

          onChangeText={TextInputEmail => this.setState({TextInputEmail})}

          // Making the Under line Transparent.
          underlineColorAndroid='transparent'

          style={styles.TextInputStyleClass}
        />

        <TextInput

          // Adding hint in Text Input using Place holder.
          placeholder="Enter Phone Number"

          onChangeText={TextInputPhoneNumber => this.setState({TextInputPhoneNumber})}

          // Making the Under line Transparent.
          underlineColorAndroid='transparent'

          style={styles.TextInputStyleClass}
        />

        <Button title="Insert Text Input Data to Server" onPress={this.InsertDataToServer} color="#2196F3" />



</View>

    );
  }
}

const styles = StyleSheet.create({

MainContainer :{

justifyContent: 'center',
flex:1,
margin: 10
},

TextInputStyleClass: {

textAlign: 'center',
marginBottom: 7,
height: 40,
borderWidth: 1,
// Set border Hex Color Code Here.
 borderColor: '#FF5722',

// Set border Radius.
 //borderRadius: 10 ,
}

});

export default ThisApp;

Here's DBConfig.php

<?php

//Define your host here.
$HostName = "localhost";

//Define your database name here.
$DatabaseName = "UserInfo";

//Define your database username here.
$HostUser = "";

//Define your database password here.
$HostPass = "";

?>

Here's submit_user_info.php:

<?php

// Importing DBConfig.php file.
include 'DBConfig.php';

// Creating connection.
 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

 // Getting the received JSON into $json variable.
 $json = file_get_contents('php://input');

 // decoding the received JSON and store into $obj variable.
 $obj = json_decode($json,true);

 // Populate name from JSON $obj array and store into $name.
$name = $obj['name'];

// Populate email from JSON $obj array and store into $email.
$email = $obj['email'];

// Populate phone number from JSON $obj array and store into $phone_number.
$phone_number = $obj['phone_number'];

 // Creating SQL query and insert the record into MySQL database table.
$Sql_Query = "insert into UserInfoTable (name,email,phone_number) values ('$name','$email','$phone_number')";


 if(mysqli_query($con,$Sql_Query)){

 // If the record inserted successfully then show the message.
$MSG = 'Data Inserted Successfully into MySQL Database' ;

// Converting the message into JSON format.
$json = json_encode($MSG);

// Echo the message.
 echo $json ;

 }
 else{

 echo 'Try Again';

 }
 mysqli_close($con);
?>
veknogl
  • 1
  • 3
  • Are you sure you have the correct hostname/port? Is your server able to access the remote host? – Blue Jan 17 '18 at 14:37
  • @FrankerZ Yeah my hostname is localhost. Where do I find port and where do I place that information? When you say is my server able to access the remote host, do you mean if the URL `http://localhost:8888/phpmyadmin/` works? If so, then yes. – veknogl Jan 17 '18 at 14:41
  • Where are you seeing `Network request failed`? – Blue Jan 17 '18 at 14:43
  • @FrankerZ Inside the simulator. I put DBConfig.php and submit_user_info.php inside the htdocs folder, forgot to mention that in my original post. – veknogl Jan 17 '18 at 14:44
  • idk what that is. Can you take a screenshot? – Blue Jan 17 '18 at 14:44
  • @FrankerZ ok, I did. It's in my post. – veknogl Jan 17 '18 at 14:47
  • Any chance you can run this in chrome/somewhere where you can see the actual request? My guess is your PHP has an error somewhere (Can't connect to your database, syntax error, or something else.) Seeing the output of the PHP script would go a long way to debug the issue. – Blue Jan 17 '18 at 14:49
  • @FrankerZ How would I run this on chrome to see the output of the PHP script? – veknogl Jan 17 '18 at 14:52
  • https://stackoverflow.com/questions/7172784/how-to-post-json-data-with-curl-from-terminal-commandline-to-test-spring-rest – Blue Jan 17 '18 at 14:53
  • You can also try: https://stackoverflow.com/a/11128594/4875631 – Blue Jan 17 '18 at 14:54
  • @FrankerZ Ok I'll try those in a bit but before I do. Do you know exactly where I can find `$HostName`, `$HostUser` and `$HostPass` in myphpadmin? – veknogl Jan 17 '18 at 14:58
  • Google my friend: https://stackoverflow.com/questions/6483268/how-to-find-out-mysql-server-ip-address-from-phpmyadmin – Blue Jan 17 '18 at 15:01
  • Did you figure it out? – Blue Jan 17 '18 at 15:42
  • @FrankerZ Nope :( – veknogl Jan 17 '18 at 16:04
  • Edit your question, or add what you've tried/the steps you've taken so far. – Blue Jan 17 '18 at 16:05

0 Answers0