0

在这段代码中,我得到了未定义的索引:类别在此我从表名 coursemaster col(course_code) 中获取值,现在当我单击上传时,下拉值不会插入到 student_table 中。所以任何人都可以帮助我。

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
             <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
              <title>Code Igniter</title>

                </head>

               <?php include_once('header.php'); ?>
                <?php include_once('menu.php'); ?>
                <br />

                <div class="gray_bg">
                <div class="container">
                <div class="row welcome_inner">
                 <div class="span12">
                <h1><span class="colored">///</span> Upload</h1>
            </div>
             </div>
              </div>
              </div>
          <div id="details">
              </div></div></center>
         <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

         File to import:<br />

         <input size='30' type='file' name='filename'>

          Enter subject code:<input type="text" name="fname1" />
          Enter subject code:<input type="text" name="fname2" />
          <input type="submit" name="submit" value="Upload"></form>




          <?php 
         $host="localhost";
         $username="root";
          $password="";
         $db_name="slseatapp"; 

          mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
         mysql_select_db("$db_name")or die("cannot select db");
         $query="SELECT id,course_code FROM `coursemaster` ORDER BY `coursemaster`.`id`";
          $result = mysql_query($query);
           ?>

          <select name="category">
          <?php
          while($nt=mysql_fetch_array($result)) {
          echo '<option value="'.$nt['course_code'].'">'.$nt['course_code'].'</option>';
           }
           ?>
           </select>



          <?php

         if($_POST){
          echo 'The course_code selected is'.$_POST['category'];
                }
          ?>



         <?php 

        mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());

           mysql_select_db("slseatapp") or die(mysql_error());

          //Upload File 
      if (isset($_POST['submit'])) { 
      if (is_uploaded_file($_FILES['filename']['tmp_name'])) { 


     //Import uploaded file to Database 
           $row = 1; 
         $handle = fopen($_FILES['filename']['tmp_name'], "r"); 
       $var = $_POST['category'];
       $var1 = $_POST['fname1'];
        $var2 = $_POST['fname2'];



         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 

          //Update Database Values 

              $import="insert into student_table (id,register_number,name,course_code,subject_code,exam_name) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."','".mysql_real_escape_string($data[2])."','$var','$var1','$var2')"; 
            $import="replace into  student_table (id,register_number,name,course_code,subject_code,exam_name) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."','".mysql_real_escape_string($data[2])."','$var','$var1','$var2')";


              mysql_query($import) or die(mysql_error()); 


              } 



           fclose($handle); 
        echo"uploaded successfully";
            }
           } 
          ?>
<br />
     <?php include_once('footer.php'); ?>
       </body>
        </html>
4

2 回答 2

1

似乎你的 HTML 并不完美,试试这个

<select name="category">
              <?php
              while($nt=mysql_fetch_array($result)) {
              echo '<option value="'.$nt['course_code'].'">'.$nt['course_code'].'</option>';
               }
               ?>
               </select>
于 2013-08-06T04:55:43.387 回答
0

首先,您错过"了 select 的名称尝试更改此设置:

<select name=category>
<?php
    while($nt=mysql_fetch_array($result)) {
        echo "<option value='".$nt['course_code']."'>".$nt['course_code']."</option>";
    }
?>
</select>

对此:

<select name="category">
<?php
    while($nt=mysql_fetch_array($result)) {
        echo "<option value='".$nt['course_code']."'>".$nt['course_code']."</option>";
    }
?>
</select>

你有太多的表格,你想更新所有的 ne。您应该只使用一个表单和一个提交按钮来上传您的所有值尝试只使用一个表单和一个提交所有值。您有一个用于 inout 类型文件的表单和一个用于选择类别的表单,您应该只使用一个。

尝试

于 2013-08-06T04:55:55.523 回答