<?php


$servername = "127.0.0.1";
$username = "root";
$password = "";
$database = "trabajoolas5";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully\n";

$allrubrics = mysqli_query($conn, 'select id, contents from `templates`');

while ($row = mysqli_fetch_array($allrubrics)) {

    foreach (json_decode($row['contents']) as $criterion) {

        $criterion_id = $criterion->id;
        $querySelect = "select * from template_criterion where template_id ={$row['id']} and criterion_id = {$criterion_id}";
        $makeQuery = mysqli_query($conn, $querySelect);
        if (!mysqli_num_rows($makeQuery)) {

            $query2 = "insert into `template_criterion` (`template_id`, `criterion_id`) values ({$row['id']}, {$criterion_id});";
            $makeQuery = mysqli_query($conn, $query2);
            if (!$makeQuery) {
                echo "Connection failed: " . mysqli_error($conn) . $query2;
            }
        }
    }
}