'How to change a column in MySQL database based on user input on a ReactNative app?
I have created a MySQL database for my React Native app and I have a table named 'schedule' in the database:
React Native app user enters a smart blind level (a number from 1 to 100) for one of the time values in the table, but since I don't know which time value is being entered I am confused as to how should I change that particular column in the table using React Native states.

For example, if a user enters 0000 for time and the corresponding blind open percentage 96, I want to change the state of time0000 to 0.96. This is proving a bit challenging.
Here's my code for the screen:
import React, { Component, useState } from "react";
import {
Text,
View,
SafeAreaView,
Platform,
StyleSheet,
TouchableOpacity,
ScrollView,
Image,
RefreshControl,
DynamicColorIOS,
TextInput,
Button,
button,
Alert,
} from "react-native";
import colors from "../config/colors";
function ScheduleInsert({ navigation }) {
const [scheduleID, setScheduleID] = useState(4);
const [time0000, setTime0000] = useState(33);
const [time1000, setTime1000] = useState(999);
const [time2000, setTime2000] = useState(999);
const [time3000, setTime3000] = useState(999);
const [time4000, setTime4000] = useState(999);
const [userID, setUserID] = useState(global.userID);
const [time1, setTime1] = useState("");
const [time2, setTime2] = useState("");
const [time3, setTime3] = useState("");
const handleChange = (timeInput, textInput) => {
let string1 = "setTime" + timeInput.toString();
string1(Number(textInput) / 100);
};
const InsertSchedule = () => {
var InsertAPIURL = "http://localhost:8080/api/scheduleWrite.php";
var headers = {
Accept: "application/json",
"Content-Type": "application/json",
};
var data = {
scheduleID: scheduleID,
time0000: time0000,
time1000: time1000,
time2000: time2000,
time3000: time3000,
time4000: time4000,
userID: userID,
};
console.log(JSON.stringify(data));
fetch(InsertAPIURL, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((response) => {
Alert.alert("You have successfully added the schedule!");
})
.catch((error) => {
Alert.alert("Error" + error);
});
};
return (
<View style={styles.container}>
<TextInput
placeholder={"Time 1 (2400Hr format. For example, 1300 for 1pm)"}
placeholderTextColor={colors.darkorange}
keyboardType={"numeric"}
style={styles.textStyle}
maxLength={4}
onChangeText={(text) => setTime1(Number(text))}
/>
<TextInput
placeholder={"Blind Open Percentage 1"}
placeholderTextColor={colors.darkorange}
keyboardType={"numeric"}
style={styles.textStyle}
maxLength={2}
onChangeText={(text) => handleChange(time1, text)}
/>
<TextInput
placeholder={"Time 2"}
placeholderTextColor={colors.darkorange}
keyboardType={"numeric"}
maxLength={4}
style={styles.textStyle}
/>
<TextInput
placeholder={"Blind Open Percentage 2"}
placeholderTextColor={colors.darkorange}
keyboardType={"numeric"}
maxLength={2}
style={styles.textStyle}
/>
<TextInput
placeholder={"Time 3"}
placeholderTextColor={colors.darkorange}
keyboardType={"numeric"}
maxLength={4}
style={styles.textStyle}
/>
<TextInput
placeholder={"Blind Open Percentage 3"}
placeholderTextColor={colors.darkorange}
keyboardType={"numeric"}
maxLength={2}
style={styles.textStyle}
/>
<Button title={"Save Schedule"} onPress={() => InsertSchedule()} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.white,
alignItems: "center",
justifyContent: "center",
},
ViewStyle: {
flex: 1,
padding: 20,
marginTop: 10,
},
textStyle: {
borderBottomWidth: 1,
borderBottomColor: "red",
marginBottom: 20,
},
});
export default ScheduleInsert;
I created this handleChange function in order to save customer's time input and then use React Native states to change the required MySQL column, but it's just not working as I get told that "setTime0000" for instance is not a function which makes sense, but I don't know how else to change my state of time0000 based on the user input.
By the way, I am able to write the default data into the database successfully, so my API is working fine.
Any help is appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
