tsp003 0 Newbie Poster

I have the following code to bulk upload to several different tables

<?php
    
    /*
        Script to bulk import users into a Joomla installation (v 1.5).                 
    */
    
     // Hande form upload
    if(isset($_POST['import'])) {
        
        $mysql_host             = trim($_POST['mysql_host']);
        $mysql_user             = trim($_POST['mysql_username']);
        $mysql_password         = trim($_POST['mysql_password']);
        $mysql_schema           = trim($_POST['mysql_schema']);
        
        $table_prefix           = trim($_POST['table_prefix']);
        
        if(!mysql_connect($mysql_host, $mysql_user, $mysql_password) || !mysql_select_db($mysql_schema)) {
            echo 'Supplied MySQL details were incorrect - aborting';
            return;
        }
        
        
        // Get the joomla groups
        $sql = sprintf('
                SELECT  `id`, `value`
                FROM    `%score_acl_aro_groups`
            ', 
            $table_prefix
        );
        $rs = mysql_query($sql);
        $groups = array();
        while($group = mysql_fetch_object($rs)) {
            $groups[$group->value] = $group->id;
        }
        
        
        $fp = fopen($_FILES['csv']['tmp_name'], 'r');
        while($user = fgetcsv($fp)) {
            
            printf('Importing "%s" ... ', $user[0]);
            
            // Lookup and verify user group
            if(!isset($groups[$user[4]])) {
                printf('error: Invalid group (%s) for %s. Defaulting to <code>Registered</code><br />%s', $user[4], $user[0], PHP_EOL);
                $user[4] = 'Registered';
            }
            
            // Insert record into users
            $sql = sprintf('
                INSERT INTO `%susers` 
                SET
                    `name`            = "%s",
                    `username`        = "%s",
                    `email`           = "%s",
                    `password`        = "%s",
                    `usertype`        = "%s",
                    `block`           = "%s",
                    `sendEmail`       = "%s",
                    `gid`             = "%s",
                    `registerDate`    = NOW(),
                    `lastvisitDate`   = "0000-00-00 00:00:00",
                    `activation`      = "",
                    `params`          = ""
                ',
                $table_prefix,
                sql_prep($user[0]),
                sql_prep($user[1]),
                sql_prep($user[2]),
                isset($_POST['md5_passwords']) ? md5($user[3]) : sql_prep($user[3]),
                sql_prep($user[4]),
                sql_prep($user[5]),
                sql_prep($user[6]),
                $groups[$user[4]]
            );
            mysql_query($sql);
            
			// Get back the user's ID
            list($user_id) = mysql_fetch_row(mysql_query('SELECT LAST_INSERT_ID()'));
            
            // Insert record into core_acl_aro
            $sql = sprintf('
                    INSERT INTO `%score_acl_aro`
                    SET
                        `section_value`   = "users",
                        `value`           = %d,
                        `name`            = "%s"
                ',
                $table_prefix,
                $user_id,
                sql_prep($user[0])
            );
            mysql_query($sql);
          
		               
			// Insert record into core_acl_groups_aro_map
            $sql = sprintf('
                    INSERT INTO `%score_acl_groups_aro_map`
                    SET
                        `group_id`        = %d,
                        `aro_id`          = LAST_INSERT_ID()
                ',
                $table_prefix,
                $groups[$user[4]]
            );
            mysql_query($sql); 
			
			// Now add in the code for employer using $user_id
            $sql = sprintf('
                    INSERT INTO %stpjobs_employer
                    SET 
                        `user_id`        =         %d,       
                        `comp_name`        =       "%s",                 
                        `id_salutation`    =       "%s",
                        `id_country`    =          "%s"
                                
                ',
                $table_prefix,
                $user_id,
                sql_prep($user[0]),
				sql_prep($user[7]),
				sql_prep($user[8])
            );
           mysql_query($sql) or die("ERROR: query: $sql: " . mysql_error());
		   
		      	   
		   // Now add the job to the job table
            $sql = sprintf('
                    INSERT INTO %stpjobs_job
                    SET 
                    `employer_id` = "",
					`Organisation` = "%s",
					`job_title` = "%s",
					`publish_date`    = NOW()    
                                                        
                ',
                $table_prefix,
				$empl_id,
                sql_prep($user[0]),
				sql_prep($user[10])
																			
           );
           mysql_query($sql) or die("ERROR: query: $sql: " . mysql_error());
            
           echo 'done.';
            flush();
        }
        
        echo '<br /><br /><strong>Done</strong>';
                
    } else {
        // show upload form
        ?>
        <html><head><title>Bulk import Jobs</title></head><body>
        <h1>Import Users to Joomla</h1>
        <p>
            Use this script to do a bulk import of users into Joomla 1.5.<br />
            Upload a CSV file with the following format:<br />
            <code>
                name, username, email, password, usertype, block, send_email
            </code><br />
            Wrap details with commas in them in quotes.
        </p>
        <hr />
        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"> 
            <input type="hidden" name="import" value="1" />
            <table cellpadding="4px">
                <tr>
                    <td>CSV File: </td>
                    <td><input type="file" name="csv" /></td>
                </tr>
                <tr>
                    <td>MD5 Hash Passwords: </td>
                    <td><input type="checkbox" name="md5_passwords" /><br /><small>*Check this option if the passwords in your CSV are in plain text</small></td>
                </tr>
                <tr>
                    <td>Joomla Table Prefix: </td>
                    <td><input type="text" name="table_prefix" value="jos_" /></td>
                </tr>
                <tr>
                    <td>Joomla Database Name: </td>
                    <td><input type="text" name="mysql_schema" value="joomla" /></td>
                </tr>
                <tr>
                    <td>MySQL Host: </td>
                    <td><input type="text" name="mysql_host" value="localhost" /></td>
                </tr>
                <tr>
                    <td>MySQL Username: </td>
                    <td><input type="text" name="mysql_username" value="" /></td>
                </tr>
                <tr>
                    <td>MySQL Password: </td>
                    <td><input type="text" name="mysql_password" value="" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value=" Import Users! " /></td>
                </tr>
            </table>
           
        </form>
        </body></html>
        <?php
    }
    
    function sql_prep($var) {
        return mysql_real_escape_string($var);
    }
	mysql_query($sql) or die("ERROR: query failure: $sql: " . mysql_error());

There are two problems that need to be addressed and i can't seem to work them out.

1. This script works, but at (line 126) "// Now add the job to the job table" entry I am getting two duplicate records created in the jobs table. All other tables are behaving normally.

2. I need to be able to check if the new user is there in the first place. If it is, to jump ahead and add the job record against that user (and employer) id, if not to go ahead and create the user.

I've been working on this for ages now and its these two things that are holding me up, but i'm stumped now, any help or pointers in the right direction would be really appreciated.

Many thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.