导入csv文件无法正常工作

时间:2014-01-03 16:25:35

标签: php html csv

我尝试了一个将我的文件夹中的csv文件导入我的SQL数据库的例子,它运行正常。但是当我将它添加到我的代码中时,它不起作用,当我点击提交按钮时,它在我的数据库表中添加了大量空记录。

以下是代码。

  1. 导入Csv。(inportall.php)

    if (isset($_POST['submit'])) {
        //echo 'File submitted'.'<br />';
    
        $file = $_FILES['file']['tmp_name'];
    
        $handle = fopen($file, "r");
    
        while (($fileop = fgetcsv($handle, 1000, ",")) !== false) {
           // echo 'Reading '.'<br />';
            $student_id = $fileop[0];
            $name = $fileop[1];
            $contact_no = $fileop[2];
            $email = $fileop[3];
            $stage = $fileop[4];
            $diploma = $fileop[5];
            $school = $fileop[6];
            $year = $fileop[7];
    
            //echo $student_id .', '.$name.', '.$contact_no.', '.$email.', '.$stage.'<br />';
    
            $sql = mysql_query("INSERT INTO student_profile (student_id,name,contact_no,email,stage,diploma,school,year) VALUES('$student_id','$name','$contact_no','$email','$stage','$diploma','$school','$year')");
        }
        if ($sql) {
            echo 'data uploaded sucessfully';
        } else {
            echo "There are duplicate copies in the database.";
        }
    }
    ?>
    
    
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title>Untitled</title>
    
         </meta>
        </head>
        <body>
            <div id="mainWrapper">
    
                <form method="post" action="importall.php" enctype="multipart/form-data">
                    <input type="file" name="file" />
                    <br/>
                    <input type="submit" name="submit" value="submit" enabled />
                </form>
                <div id="result"></div>
    
    </html>
    

    工作和不工作之间的唯一区别是,在此页面之前,我创建另一个名为studentview.php的页面

    下面:

                          查看记录                  

        <script src="_/js/myscript.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
    </head>
    <body>
    
        <?php
    
    
        include('connect-db2.php');
    
        $result = mysql_query("SELECT * FROM student_profile")
                or die(mysql_error());
    
    
    
        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>S/N</th> <th>Student_ID</th> <th>Name</th> <th>Contacts No.</th> <th>Email</th><th>Stage</th>
            <th>diploma</th><th>school</th><th>year</th></tr>";
    
    
    
        while ($row = mysql_fetch_array($result)) {
    
            echo "<tr>";
            echo '<td>' . $row['id'] . '</td>';
            echo '<td>' . $row['student_id'] . '</td>';
            echo '<td>' . $row['name'] . '</td>';
            echo '<td>' . $row['contact_no'] . '</td>';
            echo '<td>' . $row['email'] . '</td>';
            echo '<td>' . $row['stage'] . '</td>';
            echo '<td>' . $row['diploma'] . '</td>';
                echo '<td>' . $row['school'] . '</td>';
                echo '<td>' . $row['year'] . '</td>';
            echo '<td><a href="profileEdit.php?id=' . $row['id'] . '">Edit</a></td>';
            echo '<td><a href="profileDelete.php?id=' . $row['id'] . '">Delete</a></td>';
            echo "</tr>";
        }
    
    
        echo "</table>";
        ?>
        <div data-role="header"
             data-position="fixed"
             data-id="vs_header">
            <h1>View all</h1>
            <a href="adminHome.php" data-icon="home" data-iconpos="notext">Home</a>
            <a href="index.php" data-icon="delete" data-iconpos="notext">Logout</a>
        </div><!-- header -->
    <link rel="stylesheet" href="_/css/mystyles.css" />
    <p><a href="profileNew.php">Add a new record</a></p>
    
    <a href="importall.php" data-role="button" data-inline="true" data-theme="b">Import</a>
    <a href="exportall.php" data-role="button" data-inline="true" data-theme="b">Export</a>
    <?php
    echo "<input type=\"button\" value=\"Back\" onClick=\"history.go(-1);return true;\">";
    ?>
    
    
    <div data-role="footer"
         data-position="fixed"
         data-id="vs_footer">
    
        <div data-role="navbar">
            <ul>
                <li><a href="adminHome.php" 
                       data-role="button"
                       data-icon="home"
                       >Home</a></li>
                <li><a href="announcementView.php" 
                       data-role="button"
                       data-icon="arrow-r"
                       >Announcement</a></li>
                <li><a href="surveyView.php" 
                       data-role="button"
                       data-icon="arrow-r"
                       >Survey</a></li>
                <li><a href="profileView.php" 
                       data-role="button"
                       data-icon="arrow-r"
                       >Profile</a></li>
            </ul>
        </div><!-- navbar -->
    </div><!-- footer -->
    

  2. 这就是我的意思,即添加了大量空记录 http://imageshack.us/photo/my-images/585/aoys.png/

    请帮帮我..

0 个答案:

没有答案