在这段代码中,我得到了未定义的index:category,从表名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>

最佳答案

似乎您的HTML并不完美,请尝试

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

关于php - 下拉值未插入mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18072006/

10-12 03:16