PHP与echo一起使用时选择选项值

时间:2017-12-18 17:26:47

标签: php mysql mysqli

我正在尝试在表格中显示详细信息,并在下拉列表中显示值列表。值显示在表格中,但下拉列表未显示值,尽管值存在,当我将其保存在会话中并打印时,它显示在顶部。我在使用select的名称时遇到问题。当我点击按钮并想要读取选项的帖子值时,它会给我一个错误,如图

所示

enter image description here

这是代码:

for($i=0;$i<count($dr_ide);$i=$i+5)
    {
        echo "<tr>";
        echo "<td> Dr. " . $dr_namee[$i] . "</td>";
        echo "<td>" . $sub_namee[$i] . "</td>";
        echo "<td>" . $sub_namee[$i+1] . "</td>";
        echo "<td>" . $sub_namee[$i+2] . "</td>";
        echo "<td>" . $sub_namee[$i+3] . "</td>";
        echo "<td>" . $sub_namee[$i+4] . "</td>";
        echo "<td>  <select  name='perr' >
                    <option name='perr' selected=selected>Choose one</option>";
                    foreach($Names as $name) {
                        echo"<option name='perr' value=";
                             $name ;
                            echo "> ";
                            $_SESSION["tt"]=$name;
                            $name;
                            echo"</option>";


                    } 
                    echo "</select></td>";
        //echo "<td>" . $dayse[$i] . "</td>";
        //echo "<td>" . $timing[$i] . "</td>";
        echo "</tr>";
    }
}

    echo $_SESSION["tt"];

    if(isset($_POST['confirm'])){ 
        echo $_POST['perr'];
    }

这是我的完整页面代码。

<html>
<head>
<title> Expert System </title>

<link rel="stylesheet" type="text/css" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'>
 </head>
<body>
<div >
  <table id="cells2" border="0" cellpadding="15" cellspacing="5"  font size="6">

<tr>
<th><img id="header2"  src="images/Kuwait_University_Logo.jpg" > </th>
<th>KUWAIT UNIVERSITY</th>
</tr>
</table>
<div class='back1'>

<table border='1' align='center' id='customers' >
<?php
 require "init.php";
session_start();

    global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing;
    global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge;


        $query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "Subjects doesn't exist!";
    else { 
    while($row = mysqli_fetch_array($result))
        {

            $IDs[]=$row['sub_ID'];
            $Names[]=$row['Name'];
            //echo $row['Name'];
        }
    }


    $query= "SELECT * FROM test order by dr_id";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "Subjects doesn't exist!";
    else { echo "
        <tr>
            <th>Professor Name</th>
            <th>First Choice</th>
            <th>Second Choice</th>
            <th>Third Choice</th>
            <th>Fourth Choice</th>
            <th>Fifth Choice</th>
            <th>Update Subject</th>

        </tr>";

       $r=0;
       $f=0;
      while($row = mysqli_fetch_array($result))
        {
            $dr_ide[$f]=$row['dr_id'];
            $dr_namee[$f]=$row['dr_name'];
            $sub_namee[$f]=$row['sub_name'];
            $dayse[$f]=$row['days'];
            $timinge[$f]=$row['timing'];
            $fave[$f]=$row['fav'];

            //echo "<tr>";
            //echo "<td> Dr. " . $dr_namee[$f] . "</td>";
            //echo "<td>" . $sub_namee[$f] . "</td>";
            //echo "<td>" . $fave[$f] . "</td>";
            //echo "<td>" . $dayse[$f] . "</td>";
            //echo "</tr>";
            //$r++;
            $f++;
        }
        //for($i=0;$i<count($Names);$i=$i+5)
        //{
        for($i=0;$i<count($dr_ide);$i=$i+5)
        {
            echo "<tr>";
            echo "<td> Dr. " . $dr_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i+1] . "</td>";
            echo "<td>" . $sub_namee[$i+2] . "</td>";
            echo "<td>" . $sub_namee[$i+3] . "</td>";
            echo "<td>" . $sub_namee[$i+4] . "</td>";

            echo "<td>  <select  name='perr' >
                        <option name='perr' selected=selected>Choose one</option>";
                        foreach($Names as $name) {
                             echo"<option name='perr' value='$name'>$name</option>";

                        }                            
                        echo "</select></td>";
            //echo "<td>" . $dayse[$i] . "</td>";
            //echo "<td>" . $timing[$i] . "</td>";
            echo "</tr>";
        }
    }

    echo $_SESSION["tt"];

    if(isset($_POST['confirm'])){ 
        echo $_POST['perr'];
    }

?>
</table>
<form method='post' action='edit_subjects.php'>
            <input ID="btn2" name="confirm"  type="submit" value="Home">

            </form>
</div>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

name='perr'移除<options>并更改foreach(),如下所示: -

foreach($Names as $name) {
    echo"<option value='$name'>$name</option>";
} 

更改代码如下: -

echo "<form method='post' action='edit_subjects.php'>"; //add before while
        while($row = mysqli_fetch_array($result))
        {
            $dr_ide[$f]=$row['dr_id'];
            $dr_namee[$f]=$row['dr_name'];
            $sub_namee[$f]=$row['sub_name'];
            $dayse[$f]=$row['days'];
            $timinge[$f]=$row['timing'];
            $fave[$f]=$row['fav'];

            $f++;
        }
        for($i=0;$i<count($dr_ide);$i=$i+5)
        {
            $dr_nam = $dr_namee[$i];
            echo "<tr>";
            echo "<td> Dr. " . $dr_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i+1] . "</td>";
            echo "<td>" . $sub_namee[$i+2] . "</td>";
            echo "<td>" . $sub_namee[$i+3] . "</td>";
            echo "<td>" . $sub_namee[$i+4] . "</td>";

            echo "<td><select  name='perr[$dr_nam]' >
                        <option name='perr' selected=selected>Choose one</option>";
                        foreach($Names as $name) {
                             echo"<option name='perr' value='$name'>$name</option>";

                        }                            
                        echo "</select></td>";
            echo "</tr>";
        }
        echo"<input ID='btn2' name='confirm'  type='submit' value='Home'></form>"; //add just after while code ended

答案 1 :(得分:1)

echo "<td>  <select  name='perr' >
      <option name='perr' selected='selected'> Choose one </option>";
      foreach($Names as $name) {
         echo"<option name='perr' value='". $name ."'> ". $name ." </option>";
      } 
echo "</select></td>";

答案 2 :(得分:0)

你必须回复这样的名字:

foreach($Names as $name) {
    echo "<option value=";
    echo $name ;
    echo "> ";
    $_SESSION["tt"]=$name;
    echo $name;
    echo"</option>";
} 

将表格放在表格中:

<html>
<head>
<title> Expert System </title>

<link rel="stylesheet" type="text/css" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'>
 </head>
<body>
<div >
  <table id="cells2" border="0" cellpadding="15" cellspacing="5"  font size="6">

<tr>
<th><img id="header2"  src="images/Kuwait_University_Logo.jpg" > </th>
<th>KUWAIT UNIVERSITY</th>
</tr>
</table>
<div class='back1'>
<form method='post' action='edit_subjects.php'>
<table border='1' align='center' id='customers' >
<?php
 require "init.php";
session_start();

    global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing;
    global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge;


        $query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "Subjects doesn't exist!";
    else { 
    while($row = mysqli_fetch_array($result))
        {

            $IDs[]=$row['sub_ID'];
            $Names[]=$row['Name'];
            //echo $row['Name'];
        }
    }


    $query= "SELECT * FROM test order by dr_id";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "Subjects doesn't exist!";
    else { echo "
        <tr>
            <th>Professor Name</th>
            <th>First Choice</th>
            <th>Second Choice</th>
            <th>Third Choice</th>
            <th>Fourth Choice</th>
            <th>Fifth Choice</th>
            <th>Update Subject</th>

        </tr>";

       $r=0;
       $f=0;
      while($row = mysqli_fetch_array($result))
        {
            $dr_ide[$f]=$row['dr_id'];
            $dr_namee[$f]=$row['dr_name'];
            $sub_namee[$f]=$row['sub_name'];
            $dayse[$f]=$row['days'];
            $timinge[$f]=$row['timing'];
            $fave[$f]=$row['fav'];

            //echo "<tr>";
            //echo "<td> Dr. " . $dr_namee[$f] . "</td>";
            //echo "<td>" . $sub_namee[$f] . "</td>";
            //echo "<td>" . $fave[$f] . "</td>";
            //echo "<td>" . $dayse[$f] . "</td>";
            //echo "</tr>";
            //$r++;
            $f++;
        }
        //for($i=0;$i<count($Names);$i=$i+5)
        //{
        for($i=0;$i<count($dr_ide);$i=$i+5)
        {
            echo "<tr>";
            echo "<td> Dr. " . $dr_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i] . "</td>";
            echo "<td>" . $sub_namee[$i+1] . "</td>";
            echo "<td>" . $sub_namee[$i+2] . "</td>";
            echo "<td>" . $sub_namee[$i+3] . "</td>";
            echo "<td>" . $sub_namee[$i+4] . "</td>";

            echo "<td>  <select  name='perr' >
                        <option  selected=selected>Choose one</option>";
                        foreach($Names as $name) {
                             echo"<option value='$name'>$name</option>";

                        }                            
                        echo "</select></td>";
            //echo "<td>" . $dayse[$i] . "</td>";
            //echo "<td>" . $timing[$i] . "</td>";
            echo "</tr>";
        }
    }

    echo $_SESSION["tt"];

    if(isset($_POST['confirm'])){ 
        echo $_POST['perr'];
    }

?>
</table>

            <input ID="btn2" name="confirm"  type="submit" value="Home">

            </form>
</div>
</body>
</html>