Jump to content

[SOLVED] trying to read a csv file and insert individual fields into correct colunms


cluce

Recommended Posts

I have the a small table that is username(varchar 50) and password(varchar 50). And a csv file with 2 colunms such as this.....

username|password

  user1 pass1

  user2 pass2

  user3 pass3

  user4 pass4

  user5 pass5

 

I know how to read a csv in a loop and display it on a browser and use an UPDATE query to update one colunm but how can I pick out colunm 1 data(username) and put it in a string and pick out colunm 2 data(password) and put it in a string and use the INSERT INTO users(username, password) VALUES(userX, passX ) to insert the data into the database.

 

here is my code..

<?php
$counter = 1; //initialize counter

include'db.php';

$handle = fopen("pass.csv","r");
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
   $username = $data[0];   
   $password = $data[2];   

   $sql = 'INSERT INTO users (username , password) VALUES ("'.$username.'", "'.$password.'")';
   mysqli_query($mysqli, $sql);   
   $counter++;
}
?>

what its doing is inserting only the passwords and putting it in the username colunm? can someone give me some insight on this?

Link to comment
Share on other sites

OK here is what I have which I was expecting. I just dont know how to fix it

 

SQL=INSERT INTO users (username , password) VALUES ("pass1", "")

SQL=INSERT INTO users (username , password) VALUES ("pass2", "")

SQL=INSERT INTO users (username , password) VALUES ("pass3", "")

SQL=INSERT INTO users (username , password) VALUES ("pass4", "")

SQL=INSERT INTO users (username , password) VALUES ("pass5", "")

Link to comment
Share on other sites

thanks for pointing the index out I changed that but thats not all. The output is still the same as previous post.  I think the problem is somewhere in these lines. I just not sure what? .....

 

$handle = fopen("pass.csv","r");

while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)

 

and here is my whole csv file..

 

 user1   pass1

 user2   pass2

 user3   pass3

 user4   pass4

 user5   pass5

Link to comment
Share on other sites

oh. that never occured to me . maybe thats why my UPDATE loop didnt work when I tried to logon with the real passwords. so I need to change my datatype to match the hash functions.  I will try both my UPDATE and INSERT codes and see if I can update the table with the hashed passwords and see if I can login. thx again

Link to comment
Share on other sites

that was it!! I would have never figured that out by myself. Just went over my head but was simple. I also got my UPDATE loop doing the same principle to work too. All I had to do was change my varchar to 50 which I thought it was or to whatever can handle md5 or sha1.  Another careless mistake I did.

 

thx

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.