查询不起作用

I made this code:

  Acum, bifeaza materiile pe care le studiaza clasa aleasa:<br />
     <form name="servForm" action="<?php $PHP_SELF; ?>" method="post" >
    <table border="0">
  <?php  
$a = 0;
        $rezultat = "SELECT id, materie
        FROM  materii
        ORDER BY id";

        $rezultat1 = mysql_query($rezultat);
        while($plm = mysql_fetch_array($rezultat1))
            {
            if($a++ %5 == 0) echo "<tr>";
            ?>
 <td align="center"><input type="checkbox" name="checkbox2[]" value="<?php echo $plm['id']; ?>" /></td>
            <td style="text-align:left"><?php echo $plm["materie"]; ?>&nbsp;</td>
        <?php
        if($a %5 == 0) echo "</tr>";
            }
        ?>
    </table>
</div>
<br/>

    <input type="reset" value="Sterge" /> <input type="submit" value="Salveaza" name="savebtn" />
    </form>


    <?php
    if(isset($_POST['savebtn']))
    {
     foreach($_POST["checkbox2"] as $loc_id)
{
  $query = "INSERT INTO materii_pe_clase(id_scoala,id_clasa,id_materie) VALUES('$scoalalui','$clasalui','$loc_id')"; //aici cauta ! :)) 
  $result5 = mysql_query($query)
  or die('eroare');

}//sfarsit foreact
     }//sfarsit if isset

Why does the last query not work? p.s. its a school project, so mysql is ok, no need for mysqli. p.p.s I defindet the $scoalalui and $clasalui somwhere a little up the page. but they are not the problem, i tried replacing them with values. the query simply does not work. thanks!

thank you all!

EDIT

VARDUMP for $clasalui and $scoalalui :

string '1' (length=1)
string '1' (length=1)

Your problem here is that, you have error tool turned off, because PHP should have said, something like this. Notice: Undefined variable $PHP_SELF"

Since you don't see it, I'd assume that, its a root of your "problem".

PHP_SELF is not a variable, that's a constant. Its not even required here, as by default PHP sends data to its target URL.

I improved readability of your code, so that should work for you now,

<?php

// You want to see all errors? Fine:
error_reporting(E_ALL);

$a = 0;
$rezultat = "SELECT id, materie FROM  materii ORDER BY id";
$rezultat1 = mysql_query($rezultat);


// If the form is submitted, this will be executed
if (isset($_POST['savebtn'])) {
    foreach($_POST["checkbox2"] as $loc_id) {

        $query = "INSERT INTO `materii_pe_clase` (`id_scoala`, `id_clasa`, `id_materie`) VALUES('$scoalalui', '$clasalui', '$loc_id')"; 

        $result = mysql_unbuffered_query($query);

        if (!$result){
            die(mysql_error());
        }
    }

    // And finally
    die('Saved. Thanks');
}
?>

 Acum, bifeaza materiile pe care le studiaza clasa aleasa: <br />

 <form name="servForm" method="POST">
    <table border="0">
        <?php while($plm = mysql_fetch_array($rezultat1)) : ?>

        <?php if ($a++ %5 == 0) :?>
        <tr>
        <?php endif; ?>

            <td align="center">
                <input type="checkbox" name="checkbox2[]" value="<?php echo $plm['id']; ?>" />
            </td>
            <td style="text-align:left"><?php echo $plm["materie"]; ?>&nbsp;</td>

        <?php if($a %5 == 0) : ?>
        </tr>
        <?php endif; ?>

        <?php endwhile; ?>

    </table>

    <br/>

    <input type="reset" value="Sterge" />
    <input type="submit" value="Salveaza" name="savebtn" />
 </form>