Tell us what’s happening:
Describe your issue in detail here.
For this project, I have created only one table (users) in the database with fields user_id(serial, PK), user_name(varchar, not null) and other two fields.
The below bash script is not complete as I have just started it and will take more time to complete. I am not confused about the code so far because whenever I run the number_guess.sh script(with the code so far), the validation as coded is performed correctly. But every time the CodeRoad is run or initiated, two rows are inserted into the users table (that I have created) without my knowledge or action. I had to redo the code in a different way to see if the insert statement in the code /script is initiated incorrectly but that is not the case.
Can anyone explain to me why that is happening.
Your code so far
echo "Enter your username:"
read USER_NAME
#echo -e "\nThe username your enetered is $USER_NAME"
USER_ID=$($PSQL "select user_id from users where user_name='$USER_NAME'")
# if user id not available
if [[ -z $USER_ID ]]
then
echo "Welcome, $USER_NAME! It looks like this is your first time here."
#add the new user name into the table and fetch the new user id
INSERT_DETAILS=$($PSQL "insert into users(user_name,no_of_games,fewest_no_of_guess) values('$USER_NAME',0,0)")
#echo "$INSERT_DETAILS"
NEW_USER_ID=$($PSQL "select user_id from users where user_name='$USER_NAME'")
GUESS_NUMBER "MAIN-CALLING" $NEW_USER_ID 0 0
else
# if user id is available
USER_DETAILS=$($PSQL "select no_of_games,fewest_no_of_guess from users where user_id=$USER_ID")
echo "$USER_DETAILS" | while read NO_OF_GAMES BAR FEWEST_GAMES
do
echo "Welcome back, $USER_NAME! You have played $NO_OF_GAMES games, and your best game took $FEWEST_GAMES guesses."
GUESS_NUMBER "MAIN-CALLING" $USER_ID $NO_OF_GAMES $FEWEST_GAMES
done
fi
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Challenge: Number Guessing Game - Build a Number Guessing Game
Link to the challenge: