Changes in Form are not saved in Database

Hey

I have almost the same issue as in 50767. But no solution was found so here i try again. I have 2 Pages on my App, one to enter a new form and one to edit a form. The new form is saved correctly to the DB. But when i wanna save the updated form, it just closes the screen and brings me back to the page Entries and doesn’t save into the db

What am i missing?

def update_data(entry_id, data):
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("""
        UPDATE baustelle
        SET name = %s, abgang = %s, abgang_lon = %s, abgang_lat = %s, ziel = %s, ziel_lon = %s, ziel_lat = %s, beginn = %s, ende = %s, erinnerung = %s, via_ok = %s, verkauf_ok = %s, bemerkungen = %s
        WHERE id = %s
    """, data + (entry_id,))
    conn.commit()
    cursor.close()
    conn.close()

    elif page == "Entries":
        st.header("Einträge")
        date = st.date_input("Datum für Erinnerung")
        conn = get_connection()
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM baustelle WHERE erinnerung = %s", (date,))
        entries = cursor.fetchall()
        cursor.close()
        conn.close()
        
        for entry in entries:
            st.write(entry[1])  # Display 'Name der Baustelle'
            if st.button(f"Edit {entry[0]}"):
                edit_entry(entry[0])

def edit_entry(entry_id):
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM baustelle WHERE id = %s", (entry_id,))
    entry = cursor.fetchone()
    cursor.close()
    conn.close()

    st.header(f"Edit {entry[1]}")
    with st.form(key="edit_form"):
        name = st.text_input('Name der Baustelle', entry[1])
        abgang = st.text_input("Abgangsbahnhof", entry[2])
        ziel = st.text_input("Ziel", entry[5])
        beginn = st.date_input("Beginn der Baustelle", entry[8])
        ende = st.date_input("Ende der Baustelle", entry[9])
        erinnerung = st.date_input("Erinnerung", entry[10])
        via_ok = st.checkbox('via ok?', entry[11])
        verkauf_ok = st.checkbox('Verkauf ok?', entry[12])
        bemerkungen = st.text_area('Bemerkungen', entry[13])
        submitted2 = st.form_submit_button('Speichern')
        if submitted2:
            st.write("Form submitted")
            abgang_data = get_station_data(abgang)
            ziel_data = get_station_data(ziel)

            if abgang_data and ziel_data:
                data = (
                    name,
                    abgang,
                    abgang_data['lon'],
                    abgang_data['lat'],
                    ziel,
                    ziel_data['lon'],
                    ziel_data['lat'],
                    beginn,
                    ende,
                    erinnerung,
                    via_ok,
                    verkauf_ok,
                    bemerkungen
                )
                try:
                    st.write("Updating data:", data)
                    update_data(entry_id, data)
                    st.success("Eintrag erfolgreich aktualisiert!")
                except Exception as e:
                    st.error(f"An error occurred: {e}")

App runs local
DB is up and running
Python 3.12
Streamlit Version 1.32.0

Thanks for the help

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