javascript – How to insert data in database as php code is executed?-ThrowExceptions

Exception or error:

I am generating football fixtures as I press a button. After the fixtures are generated, I want those to be inserted in my database, but only after I press another button. I’m using AJAX to generate the fixtures.

HTML

<button type="submit" class="center" onclick="genereazaEtape()">GENERATE FIXTURES</button>
<button>ADD IN DB</button>
<div id="demo1"></div>

JavaScript

function genereazaEtape() {
  $('.center').click(function(e) {
    e.stopImmediatePropagation();
    e.preventDefault();
    var clickBtnValue = $(this).val();
    var ajaxurl = 'fixture.php',
    data = {
      'action': clickBtnValue
    };
    var x = document.getElementById("hidden");
    x.style.display = "block";
    $.post(ajaxurl, data, function(response) {
      $("#demo1").html(response);
    });
  });
};

fixture.php

$i = 1;
foreach ($games as $rounds) {
  $free = "";
  echo "<h5>Etapa {$i}</h5>";
  foreach ($rounds as $match) {
    if ($match[0] == "is free this round") {
      $free = "<span style='color:red;'>{$match[1]} {$match[0]}</span><br>";
      $SQL = "insert into etapa (nr_etapa, campionat) VALUES ($i, 1)";
    } elseif ($match[1] == "is free this round") {
      $free = "<span style='color:red;'>{$match[0]} {$match[1]}</span><br>";
    } else {
      echo "{$match[0]} vs {$match[1]}<br>";
    }
  }
  echo $free;
  echo "<br>";
  $i++;
}

I want the $i to be inserted in my “etapa” table in “nr_etapa” and $match[1] and $match[0] to be insterted in my “meci” table on specific position as they are in those if statements.

If the algorithm goes in first if, $match[1] should be homeClub and $match[0] should be awayClub and in second should be inverted. I don’t know where to do the SQL statement. In every If, at the end of the algorithm in the fixture.php or in my HTML file?

etapa (id_etapa, nr_etapa)
meci (id_meci, homeClub, awayClub)
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *