2016-03-25 10 views
0

PHPで新機能です。誰も私になぜDept_IDがデータベースにデータを挿入した後に表示されなかったのか教えてもらえますか?MySQLに挿入した後の未定義のID

データ閲覧ページ:私はフォームを送信した後、私はエラーが表示さ

 <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!"; 
      } 
     } 

    } 




    ?> 

:新しいレコードを追加するための

<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> 

コード:

<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>  

する新しいレコードのページを追加します。定義されていないと言っていますDept_ID

どうすればこの問題を解決できますか?

答えて

0

フォームにDept_ID要素はありません。入力name="Dept_ID"

で設定された設定されて、今働いて

<select name="Dept_ID"> 
0

あなたのコンボボックスの名前属性が指定されていません。

次のようにする必要があります:

<select name="Dept_ID"> 
+0

、ありがとう!!:場合$ _GET/$ _ POST/$ _ REQUESTデータを受け取ります –

0

この部分は、GETパラメータをエコーし​​ません。

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>"; 

あなたが代わりに変数を使用して文字列をCONCATする必要があります:

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>"; 

同様に、このw病気にも変数の値をエコーし​​ません:

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

出力:

$ DEPT_ID、$ SECTION_ID、$ Section_Desc

値をエコーするには、連結する必要がありますその後、代わりに:

echo $Dept_ID . $Section_ID . $Section_Desc; 

Dept_IDはHTMLに存在しません:

<select Dept_ID="dept"> 

があなたの代わりにnameを使用する必要があります:

<select name="Dept_ID"> 

ヒント:彼らは廃止されてmysql_*機能を使用しないでください。代わりにmysqli_*を使用してください。

+0

その作業!!!どうもありがとうございます.. –

関連する問題