Fix answer submission failing
This commit is contained in:
parent
62f2f6002b
commit
8d215f8881
56
main.go
56
main.go
@ -6,10 +6,10 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Question struct {
|
type Question struct {
|
||||||
ID int
|
ID int
|
||||||
QuestionText string
|
QuestionText string
|
||||||
@ -19,7 +19,7 @@ type Question struct {
|
|||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
|
|
||||||
func renderForm(w http.ResponseWriter, r *http.Request) {
|
func renderForm(w http.ResponseWriter, r *http.Request) {
|
||||||
rows, err := db.Query("SELECT question_text, question_type FROM questions ORDER BY question_order")
|
rows, err := db.Query("SELECT id, question_text, question_type FROM questions ORDER BY question_order")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching questions: %v\n", err)
|
log.Printf("Error fetching questions: %v\n", err)
|
||||||
http.Error(w, "Failed to fetch form questions", http.StatusInternalServerError)
|
http.Error(w, "Failed to fetch form questions", http.StatusInternalServerError)
|
||||||
@ -31,7 +31,7 @@ func renderForm(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var question Question
|
var question Question
|
||||||
err := rows.Scan(&question.QuestionText, &question.QuestionType)
|
err := rows.Scan(&question.ID, &question.QuestionText, &question.QuestionType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error scanning question: %v\n", err)
|
log.Printf("Error scanning question: %v\n", err)
|
||||||
continue
|
continue
|
||||||
@ -49,43 +49,49 @@ func handleFormSubmit(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.ParseForm()
|
err := r.ParseForm()
|
||||||
|
|
||||||
rows, err := db.Query("SELECT id FROM questions")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching questions: %v\n", err)
|
log.Printf("Error parsing form: %v\n", err)
|
||||||
http.Error(w, "Failed to fetch questions", http.StatusInternalServerError)
|
http.Error(w, "Failed to parse form", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
insertAnswerQuery := `INSERT INTO answers (question_id, answer) VALUES (?, ?)`
|
||||||
|
|
||||||
var questionID int
|
tx, err := db.Begin()
|
||||||
for rows.Next() {
|
|
||||||
err := rows.Scan(&questionID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error scanning question ID: %v\n", err)
|
log.Printf("Error starting transaction: %v\n", err)
|
||||||
|
http.Error(w, "Failed to save answers", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
} else {
|
||||||
|
tx.Commit()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for key, values := range r.Form {
|
||||||
|
if len(key) > 7 && key[:7] == "custom_" {
|
||||||
|
var questionID int
|
||||||
|
_, err := fmt.Sscanf(key, "custom_%d", &questionID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error parsing question ID from field name %s: %v\n", key, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fieldName := fmt.Sprintf("custom_%d", questionID)
|
|
||||||
fieldValue := r.FormValue(fieldName)
|
|
||||||
|
|
||||||
if fieldValue != "" {
|
answer := values[0]
|
||||||
insertQuery := `INSERT INTO answers (question_id, answer) VALUES (?, ?)`
|
_, err = tx.Exec(insertAnswerQuery, questionID, answer)
|
||||||
_, err = db.Exec(insertQuery, questionID, fieldValue)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error saving answer: %v\n", err)
|
log.Printf("Error saving answer for question %d: %v\n", questionID, err)
|
||||||
http.Error(w, "Failed to save answer", http.StatusInternalServerError)
|
http.Error(w, "Failed to save answers", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
log.Printf("No answer found for question %d", questionID)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "Thank you for your submission!")
|
fmt.Fprintf(w, "Thank you for your submission!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func handleAdmin(w http.ResponseWriter, r *http.Request) {
|
func handleAdmin(w http.ResponseWriter, r *http.Request) {
|
||||||
rows, err := db.Query(`
|
rows, err := db.Query(`
|
||||||
SELECT q.question_text, a.answer
|
SELECT q.question_text, a.answer
|
||||||
@ -142,7 +148,6 @@ func handleAddQuestion(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Redirect(w, r, "/manage", http.StatusSeeOther)
|
http.Redirect(w, r, "/manage", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func handleManage(w http.ResponseWriter, r *http.Request) {
|
func handleManage(w http.ResponseWriter, r *http.Request) {
|
||||||
rows, err := db.Query("SELECT id, question_text, question_type FROM questions ORDER BY question_order")
|
rows, err := db.Query("SELECT id, question_text, question_type FROM questions ORDER BY question_order")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -189,7 +194,6 @@ func handleRemoveQuestion(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Redirect(w, r, "/manage", http.StatusSeeOther)
|
http.Redirect(w, r, "/manage", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user