2013-08-06 83 views
0

在這段代碼中,我得到了未定義的索引:類別在這裏,我從表名coursemaster col(course_code)獲取值,現在當我單擊上傳時,下拉列表值不會插入到student_table中。所以任何人都可以幫助我。下拉的值沒有插入到mysql

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

回答

1

看來你的HTML是不完美的,試試這個

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

它顯示未定義的索引:第63行和第81行上的類別下拉選擇的值沒有上傳。 – moses

0

firsly你錯過"各地選擇嘗試的名稱更改此設置:

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

你有t很多形式,並且你想要全部更新。 您應該只使用一個表單,並且只有一個提交按鈕上傳您的所有值,儘量只使用一個表單,並且只有一個提交所有值。你有一個inout類型文件的表單和一個選擇類別的表單,你應該只爲彼此使用一個表單。

嘗試

+0

第63行和第81行中的未定義索引類別,並使student_table中的課程代碼列爲空。 – moses

+0

看到我的更新使用pnly一種形式請再試一次,bevause我認爲你點擊第一個提交按鈕,但如果你點擊第二個錯誤是在你想上傳的文件..只使用一種形式請@mooses –