Build a Salon Appointment Scheduler -- having trouble with one of the requirements:

Hello fellow FreeCodeCampers,

I’m having some trouble here. I’ve been working on a Salon Appointment Scheduler app for my certification. I’ve been having trouble with requirements 19 & 20 about “insert an appointment row”. I tried everything: double-checking my code for syntax errors, asking my dad, Microsoft Copilot, and re-writing my code twice, just to make sure I didn’t miss anything. So far, no results. I double-checked all of the column names, made sure that they were correct. This is the code that I’ve got so far –

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=salon -c"
echo -e "\n~~~~~ My Salon ~~~~~"
# display numbered menu
MAIN_MENU() {
  if [[ $1 ]]
  then
    echo -e "\n$1"
  fi
  echo -e "\nWelcome to the salon, what do you need today? \n"
  echo -e "\n1) cut \n2) color \n3) perm \n4) style \n5) trim"
  # ask for service id from the options
  read SERVICE_ID_SELECTED
  case $SERVICE_ID_SELECTED in
  1) APPOINTMENT_SCHEDULER ;;
  2) APPOINTMENT_SCHEDULER ;;
  3) APPOINTMENT_SCHEDULER ;;
  4) APPOINTMENT_SCHEDULER ;;
  5) APPOINTMENT_SCHEDULER ;;
  *) MAIN_MENU "Please enter a valid option." ;;
  esac

} 
APPOINTMENT_SCHEDULER() {
  

echo -e "\nWhat's your phone number?"
read CUSTOMER_PHONE

CUSTOMER_QUERY_RESULT=$($PSQL "SELECT customer_id FROM customers WHERE phone = '$CUSTOMER_PHONE'")



if [[ $CUSTOMER_EXISTS == "t" ]]; then 
# Customer exists 
CUSTOMER_NAME=$($PSQL   "SELECT name FROM customers WHERE phone = '$CUSTOMER_PHONE'") 
echo -e "\nWhat time would you like your appointment, $CUSTOMER_NAME?" read SERVICE_TIME 
else 
# Customer does not exist 
echo -e "\nI don't have your phone number registered yet, so what's your name?"
read CUSTOMER_NAME 
INSERT_CUSTOMER_RESULT=$($PSQL "INSERT INTO customers(phone, name) VALUES('$CUSTOMER_PHONE', '$CUSTOMER_NAME')") 
CUSTOMER_QUERY_RESULT=$($PSQL "SELECT customer_id FROM customers WHERE phone = '$CUSTOMER_PHONE'")

echo -e "\nWhat time would you like your appointment, $CUSTOMER_NAME?"

read SERVICE_TIME 
fi



  # ask for appointment time
  
echo "INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_QUERY_RESULT, $SERVICE_ID_SELECTED, '$SERVICE_TIME')"

  APPOINTMENT_RESULT=$($PSQL "INSERT INTO appointments(customer_id, service_id, time) VALUES($CUSTOMER_QUERY_RESULT, $SERVICE_ID_SELECTED, '$SERVICE_TIME')")
  SERVICE_NAME=$($PSQL "SELECT name FROM services WHERE service_id = $SERVICE_ID_SELECTED")

  echo -e "I have put you down for a $SERVICE_NAME at $SERVICE_TIME, $CUSTOMER_NAME."
 
}
MAIN_MENU

Any feedback?
Sincerely,

@cybershiloh

Welcome to the forum @cybershiloh

It looks like this echo is for debugging. You copied and pasted the code, however two of the values are missing quote marks from both the echo and APPOINTMENT_RESULT variable.

What is this variable?

Happy coding

1 Like

Good afternoon, @Teller.
Thanks for letting me know about it, I didn’t see that! I keep getting the rules of single and double quotes mixed up, ugh! :face_exhaling: :pensive:
As for your second question:

When I initially tried this piece of code:

if [[ -z $CUSTOMER_QUERY_RESULT ]]

it didn’t work. I tried adding the “tuples-only” flag to the $PSQL variable in an attempt to remove the extra formatting, but it gave me an error:

psql: warning: extra command-line argument "SELECT customer_id FROM customers WHERE phone = '555-5555'" ignored

:unamused:
So, I sought out my dad’s help and he and with the help of Copilot suggested a new variable. I also decided to remove the tuples-only flag from the $PSQL variable, I hoped that would fix the problem. It didn’t. I am not quite familiar with using the sed command in Bash yet, so I thought adding the extra flag onto the $PSQL variable would fix the problem.

If you could help me out here, that would be great. :grinning:
Sincerely,
@cybershiloh

This seems like the right approach.

Using an undefined variable won’t work.

Happy coding

1 Like

@Teller
I actually tried defining the $CUSTOMER_QUERY_RESULT variable at one point to check whether or not the customer existed. It failed (for lack of a better term) miserably. :pensive: :frowning_face:
Whenever I used the conditional, for some odd reason, it wouldn’t go through the condition at all – apparently (according to my dad) it just skipped right through the conditional (the reasons of which I’m unsure). I double checked the if-then-else-fi structure (all correct) and even had Copilot verify (which seconded my answer).

So we created a new variable ($CUSTOMER_EXISTS), but because it’s undefined, now I’m unsure of what to do. :man_shrugging:t4: :question:
Got any suggestions for a conditional or something else I could use to define the $CUSTOMER_EXISTS variable?

Confused and soooo not amused with these errors that I keep getting,
@cybershiloh

To help debug, echo the variables, see what they return.

I suspect there may be something in the customer table affecting the queries.

Okay, @Teller, I’ll try this and see what happens.

Two and a half minutes later…
Okay. I tried echoing the $CUSTOMER_QUERY_RESULT variable. This was what came up:

customer_id ------------- (0 rows)

:bulb: I could try and make this so that it checks so that it’s zero rows. Let me try that!
A few minutes later…
It works! It really works!! But now, I have a different problem: if I run the script the first time, it outputs the name properly:


But if I run it the second time, it does show the name correctly, but with a bunch of extra formatting that isn’t needed, such as the column name and lots of extra dashes, and I’m not quite sure how to remove it. :face_exhaling:

Other than that, the customer table is working fine.
:bulb: :exclamation:
All I have to do is figure out how to remove all that extra formatting! But the thing is, I tried the tuples only flag and added it to the end of the $PSQL variable, but it always gave me an error:

psql: warning: extra command-line argument "SELECT customer_id FROM customers WHERE phone = '555-5555'" ignored
ERROR:  syntax error at or near "-"
LINE 1: -t

I wish that I could know how to remove the extra formatting! Any tips?

Never mind about that! Just by me talking to you, I got it figured out! I just put the tuples-only flag in the wrong spot! :face_exhaling:

Thanks so much, @Teller!

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.