-1

Good morning friends!!

I'm trying to insert data into two tables at the same time. The first condition IF is okay, but the second that is ELSE is not fine. I am including my insertion code below. I've tried it with ELSE, with ELSEIF even with the IF's, but it is not working.

if (isset($_POST['save')) {
    if (@$_POST['id_confrontations'] == "") {
        @$description = $_POST['description'];
        @$dt_confrontation = $_POST['dt_confrontation'];
        @$id_competitions = $_POST['id_competitions'];
        @$id_stages = $_POST['id_stages'];
        @$score1 = $_POST['score1'];
        @$score2 = $_POST['score2'];
        @$mandant_club = $_POST['mandant_club'];
        @$visitor club = $_POST['visitor_club'];
        @$situation = $_POST['situation'];
        @$phase = $_POST['phase'];
        @$id_trainers = $_POST['id_trainers'];
        @$history = $_POST['history'];

        //saves the record in the "confrontations" table - In this case insertion is perfect

        $confrontations = "INSERT INTO confrontations 
                            (description, dt_confrontation, id_competitions, 
                            stadiums_id, score1, scoring2, binder_club, 
                            situation, stage, id_trainers, history) 
                VALUES ('$description','$dt_confrontation','$id_competitions',
                        '$id_estadios','$score1','$score2',
                        '$mandant_club','$visitor_club','$situation',
                        '$stage','$id_trainers','$historia')';

        //saves the record in the "panel" table - In IF insertion is perfect, but in ELSE it inserts as if it were IF
        //In the IF rule the mandating_club would be == '1', so the GF would receive the score1 and the GC would receive the score2
        //In the ELSE rule the visiting_club would be == '1', so the GC would receive the score1 and the GF would receive the score2

        if(visiting_club != '1'){
            $panel = "INSERT INTO panel (GF, GC, binant_club, visitor_club, id_competitions, status, status) 
                    VALUES ('$score1','$scor2','$mandant_club','$visitor_club',
                            '$id_competitions','$id_states','$situation')";
        }else{
            $panel = "INSERT INTO panel 
                                (GC, GF, mandant_club, visitor_club, 
                                id_competitions, status, status) 
                    VALUES ('$score1','$scor2','$mandant_club',
                            '$visitor_club','$id_competitions',
                            '$id_states','$situation')";
        }

Thank you all for your attention and suggestions.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    start with using prepared statements with parameters https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Jun 25 '20 at 19:36
  • Your quotes are mismatched. You started the `INSERT` with `"`, but you ended it with `'`. – Barmar Jun 25 '20 at 19:38
  • `visiting_club` should be `$visiting_club` -- you're missing the `$`. – Barmar Jun 25 '20 at 19:39
  • also you can use multiinsert https://stackoverflow.com/questions/1307618/multiple-mysql-insert-statements-in-one-query-php – nbk Jun 25 '20 at 19:39
  • @nbk That's only for inserting multiple rows into the same table, not different tables. – Barmar Jun 25 '20 at 19:40
  • Another typo: `$scor2` should be `$score2`. – Barmar Jun 25 '20 at 19:41
  • 1
    Way too much use of the `@` error silencer – RiggsFolly Jun 25 '20 at 20:18
  • "*is not working*" falls short of conveying any useful information about the observed behavior ... error message? too many rows added? row added but has wrong values? (StackOverflow is intended for question/answer, it is _not_ a debugging service. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – spencer7593 Jun 25 '20 at 20:57
  • @Barmar Thank you for these observations. – kemilljeans Jun 26 '20 at 12:20
  • @nbk The links you provided helped a lot. – kemilljeans Jun 26 '20 at 12:21

1 Answers1

0

Your code have huge wrong data and missing variable

  1. This variable @$visitor club = $_POST['visitor_club']; should @$visitor_club

  2. You declare variable @$history = $_POST['history']; But you call $historia variable when insert data into [confrontations] table.

  3. You start ["] double quotation for insert data into table [confrontations] but end ['] single quotation.

  4. Your insert code for table [confrontations]. You declare [11] column name but you declare [12] column value. So you should now same column name and value into data insert code.

  5. When you insert data to [panel] table, your code was below

    INSERT INTO panel (GF, GC, binant_club, visitor_club, id_competitions, status, status)

here last two column (status, status) name is same. You can't insert data using the same column name.

Note: if you want to create a insert code with MySQL. Go to PHP MyAdmin and select your database >> Select your table >> Click Insert >> Insert a dummy data >> Now MySQL generate a auto insertion code for PHP. Use this code for your insert data by change column value.

Thanks.

Obaidul Haque
  • 916
  • 11
  • 18