从表结果中获取id

时间:2015-05-23 14:52:31

标签: php html mysql

我有一个考试表,我有一个编辑部分。当我选择其中一个考试进行编辑时,它不会考试考试的ID。我无法弄明白为什么。这是我的代码:

这是我显示表格

的主要页面内容
<div class="view">
    <table class="tabel">
        <tr class="tepprand">
            <td>Data</td>
            <td>An</td>
            <td>Materia</td>
            <td>Profesor</td>
            <td>Asistent</td>
            <td>Sala</td>
            <td>Tip Examen</td>
        </tr>

        <?php
        $sqll = "SELECT r.id AS id , r.data AS data , ra.an AS an, rm.numemat AS numemat, rp1.numep AS numep1 , rp1.prenumep AS prenumep1, rp2.numep AS numep2, rp2.prenumep AS prenumep2, rs.salaa AS salaa , re.tip AS tip
                FROM examen_programat AS r
                    LEFT JOIN an ra ON (r.id_ann=ra.id_an)
                    LEFT JOIN materii rm ON (r.id_mate=rm.id_mat)
                    LEFT JOIN profesor rp1 ON (r.id_prof1=rp1.id_prof)
                    LEFT JOIN profesor rp2 ON (r.id_prof2=rp2.id_prof)
                    LEFT JOIN sala rs ON (r.id_sala=rs.id_s)
                    LEFT JOIN examen re ON (r.id_tipp = re.id_tip)
                ORDER by data";
            $result = mysqli_query($link,$sqll);

            while($rows=mysqli_fetch_array($result,MYSQLI_BOTH)) {
        ?>
        <tr>
            <td><?php echo $rows['data']; ?></td>
            <td><?php echo $rows['an']; ?></td>
            <td><?php echo $rows['numemat']; ?></td>
            <td><?php echo $rows['numep1']." ".$rows['prenumep1']; ?></td>
            <td><?php echo $rows['numep2']." ".$rows['prenumep2']; ?></td>
            <td><?php echo $rows['salaa']; ?></td>
            <td><?php echo $rows['tip']; ?></td>
            <td><a href="editexamene.php?id=<? echo $rows['id'];  ?>"> Edit </a></td>
            <td><a href="deleteexamene.php?id=<? echo $rows['id']; ?>"> Stergere </a></td>
        </tr>
        <?php
            }
        ?>
    </table>
</div>

当我按下EDIT时会发生这种情况:

<div class="view">
    <?php

            $idul=$_GET['id'];
            $str = "SELECT * FROM examen_programat WHERE id =$idul ";
            $rez = mysqli_query($link,$str);
            $row = mysqli_fetch_array($rez);
            $an=$row['id_ann'];
            $materie=$row['id_mate'];
            $profesor=$row['id_prof1'];
            $asistent=$row['id_prof2'];
            $salaa=$row['id_sala'];
            $tip=$row['id_tipp'];

        ?>

    <form name="tabel" method="post" action="updateexamen.php">
        <input type="hidden" name="idu" value="<?php echo " $idul" ?>">
        <table>
            <tr>
                <td>Data</td>
                <td><input type="date" name="data" value="<?php echo $row['data'] ?>" required="required"/><br></td>
            </tr>

            <tr>
                <td>An</td>
                <td>
                    <?php
                            $sql_year="SELECT * FROM an";
                            $rez_year = mysqli_query($link,$sql_year);
                            echo "<select name=\"year\" >";
                    while($year=mysqli_fetch_array($rez_year))
                    {
                    echo "
                    <option value=\"".$year['id_an'];
                            if ($year[
                    'id_an']==$an)
                    echo "\" selected=\"selected\">".$year['grupa']."</option>\n";
                    else
                    echo "\">".$year['grupa']."</option>\n";
                    }
                    echo "</select>";
                    ?><br>
                </td>
            </tr>

            <tr>
                <td>Materie</td>
                <td>
                    <?php
                            $sql_mat="SELECT * FROM materii";
                            $rez_mat = mysqli_query($link,$sql_mat);
                            echo "<select name=\"mat\" >";
                    while($mat=mysqli_fetch_array($rez_mat))
                    {
                    echo "
                    <option value=\"".$mat['id_mat'];
                            if ($mat[
                    'id_mat']==$materie)
                    echo "\" selected=\"selected\">".$mat['numemat']."</option>\n";
                    else
                    echo "\">".$mat['numemat']."</option>\n";
                    }
                    echo "</select>";
                    ?><br>
                </td>
            </tr>

            <tr>
                <td>Profesor</td>
                <td>
                    <?php
                            $sql_proff="SELECT * FROM profesor";
                            $rez_proff = mysqli_query($link,$sql_proff);

                            echo "<select name=\"proff\" >";
                    while($proff=mysqli_fetch_array($rez_proff))
                    {
                    echo "
                    <option value=\"".$proff['id_prof'] ;
                            if($proff[
                    'id_prof']==$profesor)
                    echo "\" selected=\"selected\">".$proff['numep']." ".$proff['prenumep']."</option>\n";
                    else
                    echo "\">".$proff['numep']." ".$proff['prenumep']."</option>\n";
                    }
                    echo "</select>";
                    ?><br>
                </td>
            </tr>

            <tr>
                <td>Asistent</td>
                <td>
                    <?php
                            $sql_profff="SELECT * FROM profesor";
                            $rez_profff = mysqli_query($link,$sql_profff);

                            echo "<select name=\"profff\" >";
                    while($profff=mysqli_fetch_array($rez_profff))
                    {
                    echo "
                    <option value=\"".$profff['id_prof'];
                            if($profff[
                    'id_prof']==$asistent)
                    echo "\" selected=\"selected\">".$profff['numep']." ".$profff['prenumep']."</option>\n";
                    else
                    echo "\">".$profff['numep']." ".$profff['prenumep']."</option>\n";
                    }
                    echo "</select>";
                    ?><br>
                </td>
            </tr>

            <tr>
                <td>Sala</td>
                <td>
                    <?php
                            $sql_sala="SELECT * FROM sala";
                            $rez_sala= mysqli_query($link,$sql_sala);

                            echo "<select name=\"sala\" >";
                    while($sala=mysqli_fetch_array($rez_sala))
                    {
                    echo "
                    <option value=\"".$sala['id_s'];
                            if ($sala[
                    'id_s']==$salaa)
                    echo "\" selected=\"selected\">".$sala['salaa']."</option>\n";
                    else
                    echo "\">".$sala['salaa']."</option>\n";
                    }
                    echo "</select>";
                    ?><br>
                </td>
            </tr>

            <tr>
                <td>Tip</td>
                <td>
                    <?php
                            $sql_type="SELECT * FROM examen";
                            $rez_type= mysqli_query($link,$sql_type);

                            echo "<select name=\"type\" >";
                    while($type=mysqli_fetch_array($rez_type))
                    {
                    echo "
                    <option value=\"".$type['id_tip'];
                            if ($type[
                    'id_tip']==$tip)
                    echo "\" selected=\"selected\">".$type['tip']."</option>\n";
                    else
                    echo "\">".$type['tip']."</option>\n";

                    }
                    echo "</select>";
                    ?><br>
                </td>
            </tr>

            <tr>
                <td><input name="submit" type="submit" value="Update"/></td>
                <td><input name="reset" type="reset" value="Reset"/></td>
            </tr>
        </table>

    </form>
</div>

这是更新:

<?php
    include ('conect.php'); 

    $idd=$_POST['idu'];
    $a1=$_POST['data'];
    $b1=$_POST['year'];
    $c1=$_POST['mat'];
    $d1=$_POST['proff'];
    $e1=$_POST['profff'];
    $f1=$_POST['sala'];
    $g1=$_POST['type'];

    $str = "UPDATE examen_programat
              SET data='$a1', 
                  id_ann='$b1',
                  id_mate='$c1',
                  id_prof1='$d1',
                  id_prof2='$e1',
                  id_sala='$f1',
                  id_tipp='$g1'
                  WHERE id='$idd'";


    $result=mysqli_query($link,$str); 

    // if successfully updated. 
    if($result){ 

    header("Location:tabelex.php");
    } 

    else { 
    echo "ERROR"; 
    } 
?>

我的examen_programat表看起来像这样:

id id_ann id_mate id_prof1 id_prof2 id_sala id_tipp

1 个答案:

答案 0 :(得分:1)

在最后两个td元素上,短打开标记可能会导致此问题。在php

之后添加?
<td>
  <a href="editexamene.php?id=<?php echo $rows['id'];?>"> Edit </a> 
</td>
<td>
  <a href="deleteexamene.php?id=<?php echo $rows['id']; ?>"> Stergere </a>  
</td>

来自PHP DOC: http://php.net/manual/en/language.basic-syntax.phptags.php

  

PHP还允许短开标记<?(因为它不鼓励   仅在使用short_open_tag php.ini启用时才可用   配置文件指令,或者如果PHP配置了   --enable-short-tags选项)。