插入MySQL后未定义的ID

时间:2016-03-25 07:37:08

标签: php

我是PHP新手。任何人都可以告诉我为什么Dept_ID在将数据插入数据库后没有显示出来。

数据查看页面:

    <html>
    <head>
    <title>View Records</title>
    </head>

    <body>
    <div id="container">
    <center> <h2> SECTION RECORD </h2> </center>
    <?php

    // connect to the database
    include('connection.php');

     // get results from database
     $result = mysql_query("SELECT * FROM section") 
     or die(mysql_error());  

        // display data in table

      echo "<table border='1' cellpadding='20' cellspacing='0' 
      align = center>";
      echo "<tr> <th>Department ID</th><th>Section ID</th> 
      <th>Section Description</th> <th> </th><th> </th>";

      // loop through results of database query, displaying them in the table
       while($row = mysql_fetch_array( $result )) {

       // echo out the contents of each row into a table

        echo "<tr>
                <td align = center>".$row['Dept_ID']."</th> 
                <td align = center>".$row['Section_ID']."</th> 
                <td align = center>".$row['Section_Desc']."</th> 

                <td><a href='Update_Dept.php?Dept_ID=$row[Dept_ID]
                &Section_ID=$row[Section_ID]
                &Section_Desc=$row[Section_Desc]'>Update</a></td>

                <td><a href='Delete_Section.php?Dept_ID=$row[Dept_ID]     
                '>Delete</a></td>

        </tr>";
    }   

 echo "<a href = 'New_Section.php'>Add new record </a>"

 ?>

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

添加新记录页:

    <form action = "New_Section_Process.php" method = "POST">
    <table border = "1"  cellpadding='10' cellspacing='0'  align = "center">
    <tr>
    <th colspan = '3'>
    <font color = "black">Section</font></th>
    </tr>

    <?php
    include("connection.php");
    ?>

    <tr>

    <td>Department ID : </td> 
    <td> <select Dept_ID="dept">
    <?php
    $query = "SELECT * FROM dept ORDER BY Dept_ID";
    $result = mysql_query($query);
    if(mysql_num_rows($result)) {
        while ($id = mysql_fetch_row($result))

            {
                echo "<option value='" . $id[1] . "'>" . $id[0] . " " . 
                $id[1] . " </option>";

            }

    } else {
        echo "<option value=''>No Department </option>";
    }
  ?>
    </select>
  </td> 
  </tr>



  <tr>
  <td>Section ID :</td>
  <td><input type = "varchar" name = "Section_ID"
  maxlength = "55" placeholder = "Enter Here">

  </td>

  </tr>
  <tr>
  <td>Section Description :</td>
  <td><input type = "varchar" name = "Section_Desc"
  maxlength = "55" placeholder = "Enter Here">
  </td>
  </tr>

  <td colspan = "3" align = "center">
  <input type = "submit" name = "submit" 
  value = "Submit">
  </td>
  </tr>

  </table>
  </form>

添加新记录的代码:

     <a href = "View_Section.php">Back Section</a>
     <form action = "New_Section_Process.php" method = "POST">
     <table align = "center">

     <?php

     $Dept_ID = '';

     //connection to database
     include("connection.php");

     $Dept_ID = $_POST['Dept_ID'];
     $Section_ID = $_POST['Section_ID'];
     $Section_Desc = $_POST['Section_Desc'];

     echo "$Dept_ID, $Section_ID, $Section_Desc";


     //creating SQL statements

     $query = "INSERT INTO section (Dept_ID, Section_ID, Section_Desc)     
     VALUES('$Dept_ID', '$Section_ID', '$Section_Desc')";

    {

    {   //run the SQL statement
        //mysql_query is the function that is used to run query
    if(mysql_query($query))
    {

        header('location:View_Section.php');
    }
    else
    {
        //error if query is not valid
        echo "Data Already Exist!";
            }
        }

    }




   ?>

提交表单后,收到错误说明未定义Dept_ID

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

您尚未指定组合框的名称属性。

应该如下:

<select name="Dept_ID">

答案 1 :(得分:0)

此部分不会回显GET参数:

flatMap

您需要使用变量连接字符串:

#!/usr/bin/python

import datetime

n = datetime.datetime.now()
sta = datetime.time(19,18)
sto = datetime.time(20,19)

if sta.hour <= n.hour and n.hour <= sto.hour:
       if sta.minute <= n.minute and sto.minute <= n.minute:
               print str(n.hour) + ":" + str(n.minute)

同样,这也不会回显变量的值:

    echo "<tr>
            <td align = center>".$row['Dept_ID']."</th> 
            <td align = center>".$row['Section_ID']."</th> 
            <td align = center>".$row['Section_Desc']."</th> 

            <td><a href='Update_Dept.php?Dept_ID=$row[Dept_ID]
            &Section_ID=$row[Section_ID]
            &Section_Desc=$row[Section_Desc]'>Update</a></td>

            <td><a href='Delete_Section.php?Dept_ID=$row[Dept_ID]     
            '>Delete</a></td>

    </tr>";

输出:

  

$ Dept_ID,$ Section_ID,$ Section_Desc

要回显这些值,您需要进行连接:

    echo "<tr>
            <td align = center>".$row['Dept_ID']."</th> 
            <td align = center>".$row['Section_ID']."</th> 
            <td align = center>".$row['Section_Desc']."</th> 

            <td><a href='Update_Dept.php?Dept_ID=" . $row['Dept_ID'] . "
            &Section_ID=" . $row['Section_ID'] . "
            &Section_Desc=" . $row['Section_Desc'] . "'>Update</a></td>

            <td><a href='Delete_Section.php?Dept_ID=" . $row['Dept_ID'] .      
            '>Delete</a></td>

    </tr>";
HTML中不存在

echo "$Dept_ID, $Section_ID, $Section_Desc";

echo $Dept_ID . $Section_ID . $Section_Desc;

您需要使用Dept_ID代替:

<select Dept_ID="dept">

提示:请勿使用name函数,因为它们已被弃用。请改用<select name="Dept_ID">

答案 2 :(得分:0)

表单中没有Dept_ID元素。如果a:input设置为name="Dept_ID"

,$ _GET / $ _ POST / $ _ REQUEST接收数据

<select name="Dept_ID">