11
tedsmith
Re: Creating users in mass - PHP Programmers Useful
  • 2005/8/15 13:24

  • tedsmith

  • Home away from home

  • Posts: 1151

  • Since: 2004/6/2 1


OK, I've taken the bull byt the horns, and tried to do it myself. I've edited Christians code to suit my own problem. Does this look like it will work as required above?

//script trouvé sur phpfrance
//http://www.phpfrance.com/tutorials/index.php?page=3&id=32
// variables
$bdd"import_test";
$host"localhost";
$user"root";
$pass"";
$fichier "users2.csv";

/* Description for data file
uname;name;email

Delete the last CRLF on last line
*/



mysql_connect($host,$user,$pass) or die("Unable to connect Database");
mysql_select_db($bdd);

// Open file for read
if (file_exists($fichier))
    
$fp fopen("$fichier""r");
else{ 
// unknown file
    
echo "File not found !
Import stopped."
;
    exit();
}
echo 
'Begin read file import '.$fichier.'
'
;
// import line by line. Re-order as per spreadhseet. 
while (!feof($fp)){
    
$ligne fgets($fp,4096);
    
$liste explode(";",$ligne);             // create array 
    
$user $liste[0];                         // 1st field - username
    
$passsprintf("%sn",md5($liste[1]));     // 2nd field - password
    
$name$liste[2];                         // 3rd field - Real Name, use 'point of contact' value 
    
$email$liste[3];                         // 4th field - E-Mail 
    
$location$liste[4];                     // 5th field - Use for the company name
    
    // Add line in xoops_users table
     
$query "INSERT INTO xoops_users  (uname, pass, name, email, user_from) VALUES('$user','$pass','$name','$e-mail','$location',)";
     
$resultmysql_query($query);
     
$uid mysql_insert_id();
         if (
mysql_error()){
          echo 
"Error in database : ".mysql_error();
          echo 
"
Import stopped."
;
          
fclose($fp);
          exit();
        }
    
//Add user in group 
    
$numgroup ='2';
     
$query "INSERT INTO xoops_groups_users_link  (groupid, uid) VALUES('$numgroup', '$uid')";
     
$resultmysql_query($query);

    
//echo 'item add = '.$uid;
     
if (mysql_error()){
      echo 
"Error in database : ".mysql_error();
      echo 
"
Import stopped."
;
      
fclose($fp);
      exit();
}else{
  echo 
"item ".$uid." - ".$user." add
 "
;
 }
}

echo 
"
Import successfully"
;

fclose($fp);


You'll see that what I've done is remove the e-mail check and creation, because in my case there is always an e-mail address anyway, so it just needs to read it in.

I've assumed that each slot in the array comprises 4096 bytes of memory, starting from slot 0.

Rather than create a password based on the username I've changed it to read in the value from the text file and then do the MD5 on it. That way the password will not be changed, yes?

Basically, in theory, if I have understood correctly, it should read in the input file column by column until it reaches the end, and then just insert (into the database) the field values (in my case, 5 columns)? Yes? Or have I got the wrong end of the stick. First attempt at PHP.

Thanks

Ted

12
LazyBadger
Re: Creating users in mass - PHP Programmers Useful

$liste = explode(",",$ligne);

according to your spec
Quis custodiet ipsos custodes?

Webmaster of
XOOPS2.RU
XOOPS Modules Proving Ground
XOOPS Themes Exhibition

13
tedsmith
Re: Creating users in mass - PHP Programmers Useful
  • 2005/8/15 14:03

  • tedsmith

  • Home away from home

  • Posts: 1151

  • Since: 2004/6/2 1


OK, thanks LazyBadger. Of course .... because my input file is a comma daliminated file.

Other than that issue, does it look like it will work? Will I have any problems connecting over the Internet (it's a remotely hosted site not a localhost) do you think? If not, I will try it out tonight. If it works, I might tweak it somemore for others to easily customise themsevles for use on XOOPS sites.

14
tedsmith
Re: Creating users in mass - PHP Programmers Useful
  • 2005/8/16 7:46

  • tedsmith

  • Home away from home

  • Posts: 1151

  • Since: 2004/6/2 1


Error received :

Quote:

Begin read file import Old_Site_Membership.csv
Error in database : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
Import stopped.


I have installed a test site using Xsas (which has worked), copies the files into the root folder, and changed the php file details re the database (such as $bdd = "xoops2", password details etc).

Any clues? Line 1 is

15
LazyBadger
Re: Creating users in mass - PHP Programmers Useful

Which version of MySQL do you have in XSAS?

Miss obvious

'$location')"
Quis custodiet ipsos custodes?

Webmaster of
XOOPS2.RU
XOOPS Modules Proving Ground
XOOPS Themes Exhibition

16
tedsmith
Re: Creating users in mass - PHP Programmers Useful
  • 2005/8/16 8:37

  • tedsmith

  • Home away from home

  • Posts: 1151

  • Since: 2004/6/2 1


IT WORKS! IT WORKS!! IT WORKS!!! IT WORKSsssssssssssss!!!!!

I'm so happy I could cry!! My first ever attempt at a serious modification of a PHP file, in with a little help from lazybadger (for which I am every gratful) it's worked!!

Now all I need to do is run it on my live site and hope that it works on that too!!

Thanks again...to JDseymour, LazyBadger, Christian, and anyone else who has helped in this thread!



Ted

17
tedsmith
Re: Creating users in mass - PHP Programmers Useful
  • 2005/8/16 9:43

  • tedsmith

  • Home away from home

  • Posts: 1151

  • Since: 2004/6/2 1


Yep...it works on the live site too!! All my members successfully imported from my csv file holding 200 or so members. Every now successfully a member of my 'Registered Users' group.

Myself and JDseymour are going to work on this somemore together to try and make it more customisable, probably with the use of html forms. But this won't be for a while.

In the meantime, if anyone needs it, PM me.

Ta

Ted

Login

Who's Online

339 user(s) are online (206 user(s) are browsing Support Forums)


Members: 0


Guests: 339


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits