I’m having a lot of problems with my little tool that uploads CSV files to SQL databases. Here is a sample csv file (in an actual csv file, obviously, but I just put the original csv text so that you can see my table easier):
login_name,password,permission
super_admin,admin123,edit admins and export user data (superadmin)
admin,admin123,sell tickets only
Here is a portion of my script:
[php]<?php
include_once ‘include/config.php’;
include ‘include/header.php’;
$user_id = NULL;
if (isset($_POST[‘btn_submit’]) && $_POST[‘btn_submit’] == ‘Upload Multiple’) {
$path = $_FILES[‘upload_file’][‘name’];
$ext = pathinfo($path, PATHINFO_EXTENSION);
if ($ext != ‘csv’) {
header(‘Location:add_admin.php?op= addition problem. Only CSV file is allowed&message=ok&type=’ . $_POST[‘current_member’]);
exit;
}
if ($_FILES[‘upload_file’][‘size’] > 0) {
//get the csv file
$file = $_FILES[‘upload_file’][‘tmp_name’];
$handle = fopen($file, “r”);
$first_line = TRUE;
$adminids = array();
//loop through the csv file and insert into database
while ($data = fgetcsv($handle, 1000, “,”, “’”)) {
if ($first_line) {
$first_line = FALSE;
continue;
}
$permission = $data[2] == ‘edit admins and export user data (superadmin)’ ? 1 : 2;
if ($data[0] && $data[0] != ‘’) {
try {
$login_name = $data[0];
$alreadyExist = mysql_result(mysql_query(“SELECT COUNT() FROM users WHERE login_name=’$login_name’"), 0);
if ($alreadyExist > 0)
continue;
$sql = “INSERT INTO users (login_name,password,p_id) VALUES
(
'” . addslashes($data[0]) . “’,
'” . addslashes(md5($data[1])) . “’,
'” . addslashes($permission) . “’)”;
mysql_query($sql);
} catch (Exception $ex) {
continue;
}
$adminids[] = mysql_result(mysql_query(‘Select LAST_INSERT_ID();’), 0);
}
}
}
} else {
if ($_POST) {
$admin_username = $_POST[‘admin_username’];
$admin_password = $_POST[‘admin_password’] != ‘’ ? md5($_POST[‘admin_password’]) : ‘’;
$provision = $_POST[‘provision’];
$InsertQuery = “INSERT INTO users(login_name,password, p_id)VALUES (’$admin_username’, ‘$admin_password’, ‘$provision’)”;
if (isset($_POST[‘user_id’]) && trim($_POST[‘user_id’]) != ‘’) {
$user_id = $_POST[‘user_id’];
$InsertQuery = “UPDATE users SET login_name=’$admin_username’,password=’$admin_password’,p_id=$provision WHERE user_id=$user_id;”;
if ($admin_password == ‘’) {
$InsertQuery = “UPDATE users SET login_name=’$admin_username’,p_id=$provision WHERE user_id=$user_id;”;
}
$queryResult = mysql_query($InsertQuery);
} else {
$alreadyExist = mysql_result(mysql_query("SELECT COUNT() FROM users WHERE login_name=’$admin_username’”), 0);
if ($alreadyExist > 0) {
header(‘Location:add_admin.php?message=username already exist please choose different username’);
exit;
}
$queryResult = mysql_query($InsertQuery);
$user_id = mysql_result(mysql_query(‘Select LAST_INSERT_ID();’), 0);
}
}
}
?>[/php]
It adds a user ID to each person for the first column.
The result is that the login_name and password columns remain empty in the SQL table. Permission column and user_id column are entered correctly. BUT only the first row is read, nothing more. So only one entry is entered into the SQL table, not each entry from the csv file.
Somewhere I am off, but I am a beginner and cannot figure this out. Can someone help me? Thank you so much.Again, beginner, so any explanations are appreciated:)
Thanks again.