0

I have a table membership where I'm saving all paid or owed months for every user, but I have a problem where member A can pay January 2019 or any other month unlimited amount of times. I need to check if member A (or whatever ID is for another member) paid for January 2019 (or any other month/year) and if he did, upon making same payment (January 2019) to throw an error that member A paid that month in 2019 already.

private void Plati_Clanarinu_tipka_Click(object sender, EventArgs e)
    {
        MySqlConnection connection;
        string conString = "server=localhost; database=bazakudsumari; uid=David; password=root";
        try
        {
            connection = new MySqlConnection
            {
                ConnectionString = conString
            };
            connection.Open();

            if (string.IsNullOrWhiteSpace(iznos_clanarine.Text) || string.IsNullOrWhiteSpace(odaberi_mjesec.Text) || (!placeno.Checked && !duzan.Checked) || string.IsNullOrWhiteSpace(godina_uplate_clanarine.Text) || string.IsNullOrWhiteSpace(ime.Text) || string.IsNullOrWhiteSpace(prezime.Text) || string.IsNullOrWhiteSpace(datum_rodenja.Text) || string.IsNullOrWhiteSpace(oib.Text))
            {
                MessageBox.Show("Odaberite člana i ispunite sva potrebna polja prije naplate članarine!");
            }
            else
            {
                string Query = "INSERT INTO evidencija_clanarina (id_clana, mjesec, godina, vrsta, iznos, dug) VALUES (@id_clana, @mjesec, @godina, @vrsta, @iznos, @dug)";
                using (MySqlCommand command = new MySqlCommand(Query, connection))
                {
                    command.Parameters.AddWithValue("@id_clana", Convert.ToInt32(id_clana.Text));
                    command.Parameters.AddWithValue("@mjesec", odaberi_mjesec.Text);
                    command.Parameters.AddWithValue("@godina", godina_uplate_clanarine.Text);
                    if (placeno.Checked)
                    {
                        command.Parameters.AddWithValue("@dug", placeno.Text);
                    }
                    else
                    {
                        command.Parameters.AddWithValue("@dug", duzan.Text);
                    }
                    if (mjesecna_clanarina.Checked)
                    {
                        command.Parameters.AddWithValue("@vrsta", "Mjesečna");
                        command.Parameters.AddWithValue("@iznos", iznos_clanarine.Text);
                    }
                    else
                    {
                        command.Parameters.AddWithValue("@vrsta", "Godišnja");
                        command.Parameters.AddWithValue("@iznos", iznos_clanarine.Text);
                    }

                    command.ExecuteNonQuery();
                }

                MessageBox.Show("Uspješno plaćena članarina!");

                DialogResult dialogResult = MessageBox.Show("Želite li naplatiti još jednu članarinu?", "", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    this.Focus();
                }
                else
                {
                    this.Close();
                }

                odaberi_mjesec.Text = string.Empty;
                iznos_clanarine.Text = string.Empty;
                godina_uplate_clanarine.Text = string.Empty;
                mjesecna_clanarina.Checked = false;
                godisnja_clanarina.Checked = false;
                placeno.Checked = false;
                duzan.Checked = false;
                baza_podataka_clanovi_clanarine.Refresh();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
  • Possible duplicate of [How to 'insert if not exists' in MySQL?](https://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql) – devlin carnate Oct 07 '19 at 16:03
  • We need a [mcve], **minimal** is important. Can you please narrow down your issue? What have you tried? What is not working? Also, consider reading our [ask] topics. – dymanoid Oct 07 '19 at 16:04
  • @dymanoid Everything is working, I just need to implement that if member A paid January that he cannot pay January again. I haven't tried anything yet, I was googling to find out how to do it, but couldn't find anything so I came here to ask. I tried just now INSERT IGNORE but it still saved data for Member A for January. – David Hruškar Oct 07 '19 at 16:11

0 Answers0