我在React Native中是一个新手。 我有登录和注册页面,它们运作良好。 用户登录后,个人资料页面应显示Welcome +他的名字。 我写了一个前端和后端代码,但是工作不正常。
Profile.js
export default class Profil extends React.Component {
constructor(props){
super(props);
this.state={name:''}
};
componentWillMount(){
return this.getUser();
}
getUser(){
return fetch('https://lifestormweb.000webhostapp.com/profile.php',{
method:'GET',
headers:{
'Accept':'application/json',
'Content-Type':'application/json',
}
})
.then((response)=>response.json())
.then((response) => this.setState({
name: response.name})
)
}
render() {
return (
<View>
<Header
centerComponent={{ text: 'Profil', style: { color: '#FF0000', fontSize: 20, fontWeight: 'bold' } }}
outerContainerStyles={{ backgroundColor: '#ffffff' }}
/>
<Text>Willkommen {this.state.name}</Text>
</View>
);
}
profile.php
<?php
$dbservername = "id6354774_users";
$dbusername = "id6354774_root";
$dbpassword = "*********";
$dbname = "localhost";
// Create connection
$conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$post_name = mysqli_real_escape_string($conn, $_POST['name']);
$sql = "SELECT * FROM UserRegistrationTable WHERE name='$post_name'";
$result = $conn->query($sql);
$conn->close();
?>