doujiaoben28596 2013-10-02 14:12
浏览 25
已采纳

查询不起作用

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)
  • 写回答

1条回答 默认 最新

  • duanfei8399 2013-10-06 13:16
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作