我似乎无法找到为什么这段代码不起作用

时间:2013-10-08 10:33:46

标签: php mysql forms while-loop session-variables

免责声明这是一个学校项目,所以MYSQL可以使用。

所以,我想使用一些post方法表单。我现在编写并重写了这段代码3次。但它似乎并没有很好地发布帖子值...进入帖子...而且,我希望一旦选择一个表格消失(为了不混淆用户)。我设法使用or, ||运算符在另一个版本中执行此操作但在此版本中我不使用它以便保持简单。

这是代码

$clasa_elevului = $_SESSION['clasa'];
$scoala_elevului = $_SESSION['scoala'];
$judetul_elevului = $_SESSION['judet'];



?>
<a href="http://localhost/index.php?ex=8"> ALEGE ALT ELEV! </a><br />
<?php
if(isset($_POST['submit93']))
{

    $_SESSION['elevi'] = $_POST['elevi'];
}


else //daca nu am apasat butonul de alegere a elevului
{
$alegeelevul = "SELECT cod_elev, prenume_elev
FROM elevi
WHERE id_clasa = $clasa_elevului AND id_scoala = $scoala_elevului AND id_judet = $judetul_elevului
ORDER BY cod_elev";
$elevul = mysql_query($alegeelevul);
?>
Alege elevul:

<form method="post" action="" name="form40">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>

    <td  width="150"><select name="scoala">
<option value="">Alege elevul</option>
   <?php
 while($elevii = mysql_fetch_array($elevul))
{

      echo "<option value='".$elevii['cod_elev']."' name='elevi'>" . $elevii['prenume_elev'] . "</option>";

  }
  ?>
  </select>
  </td></tr>

  </table> <br />
     <input type="submit" name="submit93" value="&nbsp;Alege &nbsp;" >
     </form>


   <?php 
   }  

   /* PANA AICI TINE ELSE DACA NU AM APASAT BUTONUL DE TRIMITERE. DE AICI IN JOS ESTE MATERIA !!! DE AICI IN SUS E ELEVUL!!!!  */
   ?>  

  <?php if(isset($_POST['submit10']))//daca e apasat butonul submit 10
  {
      $_SESSION['materii'] = $_POST['materii'];
  }

  else
  {
      ?>

    Acum alege materia :
<?php
$alege_materia = "SELECT
  dmedia_didact.materii.id AS id,
  dmedia_didact.materii.materie
FROM
  dmedia_didact.materii INNER JOIN
  dmedia_didact.materii_pe_clase ON dmedia_didact.materii_pe_clase.id_materie =
    dmedia_didact.materii.id
    WHERE dmedia_didact.materii_pe_clase.`id_clasa` = $clasa_elevului AND dmedia_didact.materii_pe_clase.`id_scoala` = $scoala_elevului ";
    $materia = mysql_query($alege_materia);
?>
<form method="post" action="" name="form60">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>

    <td  width="150"><select name="scoala">
<option value="">Alege materia</option>
   <?php
 while($materii = mysql_fetch_array($materia))
{

      echo "<option value='".$materii['id']."' name='materii'>" . $materii['materie'] . "</option>";

  }
  ?>
  </select>
  </td></tr>

  </table> <br />
     <input type="submit" name="submit10" value="&nbsp;Alege &nbsp;" >
     </form><br />
<?php
  } //sfarsit else daca NU am apasat submit 10 
if(isset($_SESSION['elevi']))
{
echo $_SESSION['elevi'];    
}

请注意前3个会话变量正常工作。表单显示了它们的数据库内容。

它出了什么问题?谢谢!

编辑:错误如下所示:Notice: Undefined index: materii in C:\wamp\www\index.php on line 620

第620行是这一个:

 <?php if(isset($_POST['submit10']))//daca e apasat butonul submit 10
  {
      ***$_SESSION['materii'] = $_POST['materii'];***
  }

FOOLISH ME !!!我通过查看此处的代码解决了问题。问题是我没有在帖子中使用选择名称标签,但我使用了选项名称标签。现在正在工作!

2 个答案:

答案 0 :(得分:2)

这不是<select>的工作方式。

假设你有

<select name="nameOfTheSelect">
<option value="x">sample value</option>
<option value="y">another value</option>
</select>

并且用户选择第二个值(另一个值)并发布它,您将收到它:

$_POST['nameOfTheSelect'] == 'y';

因此,<option>没有自己的名字,因为它是一个值。您在此处没有$_POST['y'](在您的情况下为$_POST['materili']

答案 1 :(得分:1)

您需要更改以下内容:

<select name="scoala">
<option value="">Alege elevul</option>
   <?php
 while($elevii = mysql_fetch_array($elevul))
{    
      echo "<option value='".$elevii['cod_elev']."' name='elevi'>" . $elevii['prenume_elev'] . "</option>";

  }
  ?>
  </select>

<select name="elivi">
<option value="">Alege elevul</option>
   <?php
 while($elevii = mysql_fetch_array($elevul))
{

      echo "<option value='".$elevii['cod_elev'].">" . $elevii['prenume_elev'] . "</option>";

  }
  ?>
  </select>

name上不能拥有<option>属性。 name应该放在<select>