The error checking here tells me the file exist since no error is displayed. The table is created as well so I know this part of the code is executing.
$handle = fopen("$uploadFolder\$fileName", "r");
if (!$handle) {echo("<p>Error opening file:</p>");}
$table_name = "_tl_session_grade_import";
$sql = "CREATE TABLE IF NOT EXISTS ".$prefix."$table_name (
`ReportNumber` int(3) NULL,
`Student_ID` varchar(9) NULL,
`Name_Last` varchar(50) NULL,
`Name_First` varchar(30) NULL,
`Total_RS` tinyint(4) NULL,
`Total_Percent` tinyint(3) NULL,
`Percentile` tinyint(3) NULL,
`Subtest_RS` tinyint(3) NULL,
`Subtest_Percent` tinyint(3) NULL
)";
$result = $db->sql_query($sql);
if (!$result) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
Thanks for you help troubleshooting.
Here is the script with your suggestions in it's entirety:
<?php
/************************************************************************/
/* Academic Team Learning & Assessment System */
/* ========================================== */
/* Author: Steven Donovan */
/* Web Developer */
/* Boonshoft School of Medicine */
/* Project Start Date 10/25/2007 */
/* */
/************************************************************************/
global $admin_file, $db, $prefix;
if(!defined('TL_ADMIN')) { die("Illegal Access Detected!!!"); }
$modname = basename(str_replace("/admin", "", dirname(__FILE__)));
require_once("mainfile.php");
include("header.php");
TLAdminMenu();
Opentable();
echo" <h2 align=\"center\">IRAT CSV import</h2>";
echo " <table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\">";
echo "<tr><b>Steps to import</b></tr>"
. "<tr><ul>"
. "<tr><li>Open the Excel File</tr>"
. "<tr><li>Remove the first row of header information containing ReportNumber, UID, StudentName, etc.</tr>"
. "<tr><li>Save file as a csv file. File/Save As/Choose csv format (Comma Delimited) in dropdown box.</tr>"
. "</ul>";
echo "<form action='".$_server['php_self']."' method='post' enctype=\"multipart/form-data\">"
. " <table border=\"1\" align=\"center\">"
. " <tr>"
. " <td>Source CSV file to import:</td>"
. " <td rowspan=\"30\" width=\"10px\"> </td>"
. " <td><input type=\"file\" name=\"file_source\" id=\"file_source\" class=\"edt\" value=\"$file_source\"></td>"
. " </tr> "
. " <tr>"
. " <td colspan=\"3\" align=\"center\"><input type=\"Submit\" name=\"Go\" value=\"Next Step\" onClick=\" var s = document.getElementById('file_source'); if(null != s && '' == s.value) {alert('Define file name'); s.focus(); return false;}\"></td>"
."<input type='hidden' name='op' value='TL_CSV_Import'>\n"
. "<input type='hidden' name='Session_ID' value='$Session_ID'>\n"
. " </tr>"
. " </table>"
. " </form>"
."";
Closetable();
if(isset($_POST["Go"]) && ""!=$_POST["Go"]) {
$insert_array=array();
$uploadFolder = $_SERVER['DOCUMENT_ROOT'] . "/atlas/modules/$modname/uploadcache/";
$fileName = basename($_FILES['file_source']['name']);
move_uploaded_file($_FILES['file_source']['tmp_name'], $uploadFolder.$fileName);
$handle = fopen("$uploadFolder\$fileName", "r");
if (!$handle) {echo("<p>Error opening file:</p>");}
$table_name = "_tl_session_grade_import";
$sql = "CREATE TABLE IF NOT EXISTS ".$prefix."$table_name (
`ReportNumber` int(3) NULL,
`Student_ID` varchar(9) NULL,
`Name_Last` varchar(50) NULL,
`Name_First` varchar(30) NULL,
`Total_RS` tinyint(4) NULL,
`Total_Percent` tinyint(3) NULL,
`Percentile` tinyint(3) NULL,
`Subtest_RS` tinyint(3) NULL,
`Subtest_Percent` tinyint(3) NULL
)";
$result = $db->sql_query($sql);
if (!$result) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
$checktable = $db->sql_query("SELECT * FROM ".$prefix."$table_name");
$table_total = $db->sql_numrows($checktable);
//We already have records in table
if ($table_total > 0) {
echo "<form action='".$_server['php_self']."' method='post'";
OpenTable();
echo"<tr><td>There are records already loaded from a previous file (TL Session).</td></tr>";
echo"<tr><td>You can view these IRAT grades <a href='".$admin_file.".php?op=TLImportIratView&Session_ID=$Session_ID'>Here!</a></td></tr>";
echo"<tr><td>Or click Continue to empty this table and start over.</td></tr>";
echo"<tr>"
. "<td align=\"center\"><input type=\"Submit\" name=\"TruncateTable\" value=\"Continue\"></td>"
. "</tr>";
Closetable();
echo"</form>";
die();
}
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$insert_explode_array=explode(",",$buffer);
$insert_item="(\"".$insert_explode_array[0]."\",
\"".$insert_explode_array[1]."\",
\"".$insert_explode_array[2]."\")";
array_push($insert_array,$insert_item);
}
fclose($handle);
$query="INSERT INTO ".$prefix."$table_name ('ReportNumber', 'Student_ID', 'Name_Last', 'Name_First', 'Total_RS', 'Total_Percent', 'Percentile', 'Subtest_RS', 'Subtest_Percent') VALUES ";
$result = $db->sql_query($query);
$item_count=count($insert_array);
for($i=1;$i<=$item_count-1;$i++)
{
$query .= array_pop($item_array) .","; // adds items to the insert statement
}
$query .= array_pop($item_array); // finishes the insert statement
}
if(mysql_affected_rows() != count($item_count)) {
die('MySQL Insert failed to insert all entries.');
}else{
header("Location: ".$admin_file.".php?op=TLScantronIratInsert&Session_ID=$Session_ID");
}
}
?>