Jump to content

CSV Input script


grazzman

Recommended Posts

So, below is the script I'm trying to use to import a CSV data. the clsuser.php contains my mysql connection information an is working on all my other pages, so im sure its not in their. When I run the script the "mysql_query($sql, $mysql_conn);" in the middle is not working. It keeps showing the error message that I put in their and none of the information is added to the database. If anyone can help and can see what little thing is causing this, it would be a big help. Thanks

[code]

<?php
require_once "clsuser.php";

$a=new Auth($mysql_conn);

$csvfile="pm.csv";

$csv=file($csvfile);

foreach($csv as $csvline){
$csvdat=QuoteExplode($csvline);

$userinfo=array();
$userinfo['Username']=$csvdat[0];
$userinfo['Password']=$csvdat[2];
$userinfo['Email']=$csvdat[10];
$userinfo['FirstName']=$csvdat[8];
$userinfo['Location']=$csvdat[11];
$userinfo['SignupTime']=date("Y-m-d H:i:s", strtotime($csvdat[4]));
$userinfo['LastLogin']=date("Y-m-d H:i:s", strtotime($csvdat[3]));
$userinfo['RegistrationIP']=ip2long($csvdat[5]);
$userinfo['LastName']=$csvdat[9];
$userinfo['BirthDay']=$csvdat[12];
$userinfo['BirthMonth']=$csvdat[13];
$userinfo['BirthYear']=$csvdat[14];
$userinfo['Gender']=$csvdat[15];
$userinfo['Xfire']=$csvdat[16];
$userinfo['GameName']=$csvdat[18];
$userinfo['Recruiter']=$csvdat[20];
$userinfo['LastClan']=$csvdat[21];
$userinfo['Connection']=$csvdat[23];
$userinfo['PCType']=$csvdat[24];
$userinfo['Status']=$csvdat[34];
$userinfo['ForumsID']=$csvdat[35];
$userinfo['Rank']=$csvdat[31];
$userinfo['RankType']=$csvdat[32];
$userinfo['RegTeam']=$csvdat[27];
$userinfo['RegTitle']=$csvdat[30];
$userinfo['CICTeam']=$csvdat[38];
$userinfo['CICTitle']=$csvdat[39];
$userinfo['MPTeam']=$csvdat[26];
$userinfo['MPTitle']=$csvdat[29];
$userinfo['SFTeam']=$csvdat[40];
$userinfo['SFTitle']=$csvdat[41];
$userinfo['CTeam']=$csvdat[25];
$userinfo['CTitle']=$csvdat[28];



$userinfo['AUTHSKEY']=$a->sKeyGenerate($userinfo['Username']);

$dbtime=date(DBdateformat);

//var_dump($userinfo);
//continue;

$sql="INSERT INTO User SET
Username='" . $userinfo['Username'] . "',
Password='" . $userinfo['Password'] . "',
Email='" . $userinfo['Email'] . "',
FirstName='" . $userinfo['FirstName'] . "',
Location='" . $userinfo['Location'] . "',
EmailVerified=1,
AUTHSKEY='" . $userinfo['AUTHSKEY'] . "',
SignupTime='" . $userinfo['SignupTime'] . "',
LastLogin='" . $userinfo['LastLogin'] . "',
LastName='" . $userinfo['LastName'] . "',
BirthDay='" . $userinfo['BirthDay'] . "',
BirthMonth='" . $userinfo['BirthMonth'] . "',
BirthYear='" . $userinfo['BirthYear'] . "',
Gender='" . $userinfo['Gender'] . "',
Xfire='" . $userinfo['XFire'] . "',
GameName='" . $userinfo['GameName'] . "',
Recruiter='" . $userinfo['Recruiter'] . "',
LastClan='" . $userinfo['LastClan'] . "',
Connection='" . $userinfo['Connection'] . "',
PCType='" . $userinfo['PCType'] . "',
Status='" . $userinfo['Status'] . "',
ForumsID='" . $userinfo['ForumsID'] . "',
Rank='" . $userinfo['Rank'] . "',
RankType='" . $userinfo['RankType'] . "',
RegTeam='" . $userinfo['RegTeam'] . "',
RegTitle='" . $userinfo['RegTitle'] . "',
CICTeam='" . $userinfo['CICTeam'] . "',
CICTitle='" . $userinfo['CICTitle'] . "',
MPTeam='" . $userinfo['MPTeam'] . "',
MPTitle='" . $userinfo['MPTitle'] . "',
SFTeam='" . $userinfo['SFTeam'] . "',
SFTitle='" . $userinfo['SFTitle'] . "',
CTeam='" . $userinfo['CTeam'] . "',
CTitle='" . $userinfo['CTitle'] . "',
RegistrationIP=" . $userinfo['RegistrationIP'] . ";";


$qres=mysql_query($sql, $mysql_conn);
if (!$qres)
echo 'Error inserting user with username '.$userinfo['Username'].'<br />';
else
echo 'Inserted user with username '.$userinfo['Username'].'<br />';
}


function QuoteExplode($str){
$c=0;
$ret=array();
$str="," . trim($str);
while($c<strlen($str)){
$so=strpos($str, ",", $c);
if ($so===false){
$ret[]=substr($str, $c);
return $ret;
}
$qo=strpos($str, "\"", $c);
if ($so<$qo && $so!==false){

}
if ($qo!==false && $qo<$so+2){ // Next it is a quote mark
$eqo=strpos($str, "\"", $qo+1);
if ($eqo===false){
$ret[]=substr($str, $qo+1);
return $ret;
}
$ret[]=substr($str, $qo+1, $eqo-$qo-1);
$c=$eqo+1;
}
else{
$eso=strpos($str, ",", $so+1);
if ($eso===false){
$ret[]=substr($str, $so+1);
return $ret;
}
$ret[]=substr($str, $so+1, $eso-$so-1);
$c=$eso;
}
}
return $ret;
}
?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/31842-csv-input-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.