I have created a CSV upload oprtion for my webpage and now I am writing a Php code for action to post data in to mysql.
PHP Code:
<?php session_start(); $host="localhost"; // Host name $username="******"; // Mysql username $password=""; // Mysql password $db_name=""; // Database name $tbl_name="new_user_verf"; // Table name $myusername = $_SESSION['myusername']; //user who updating $con = mysql_connect("$host","$username","$password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); $fp = fopen("C:\Users\Desktop\new_user_data.csv", "r"); $length = 8096; /// have to be optional length ... $fildDelineate = ','; /// or "|" ... declare what you need $counter = 1; // to omission first row if it is table headers while( !feof($fp) ) { if( !$line = fgetcsv($fp, $length, $fildDelineate, '') && $counter) { continue; } foreach($line as $key => $value){ // For example insert in 3 column (e.g. id, name, email ) $importSQL = "insert into $tbl_name values('$line[0]','$line[1]','$line[2]','$line[3]','$line[4]','$line[5]','$line[6]','$line[7]','$line[8]','$line[9]')"; mysql_query($importSQL) or die(mysql_error()); } $counter++; } { header("location:new_user.php") or die("record not inserted"); } fclose($fp); ?>
The code above is generating below errors pls help me.
Code:
Warning: fopen(C:\Users\Desktop ew_user_data.csv) [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\SAM\csv_upload.php on line 11
Code:
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SAM\csv_upload.php on line 16
Warning: feof() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SAM\csv_upload.php on line 15
Please help me !!!!....and suggest what mistake am I doing here!!
Regards,
Nani
Last edited by nani_nisha06; 10-08-2012 at 03:47 PM..
Do not end a directory name with a space or a period. You did. The Desktop in this line throws me also. To make things simple why not put new_user_data.csv into the C: dir and go from there?
Do not end a directory name with a space or a period. You did. The Desktop in this line throws me also. To make things simple why not put new_user_data.csv into the C: dir and go from there?
But in realty if the user try to upload from desktop then what I guess the issue would be the same so can you help me to fix this ???
Uh, what?
If you upload, you get the path from the $_FILES superglobal. It is explicit in a chosen temp directory and the filename will be some randomness. You cannot access a remote filesystem.
The problem here is the use of \. \ followed by another character may be a command which is interpreted in double quotations. Escape the slash with a \ or convert them to front slashes.
The rest of the errors are because you didn't implement any type of error controlling. You simply assume that the resource is valid and opened successfully instead of checking that it did. Resource handles will return false if they fail, or a resource if they are successful. Block them in branches to verify if they are successful.
The problem here is the use of \. \ followed by another character may be a command which is interpreted in double quotations. Escape the slash with a \ or convert them to front slashes.
Better yet put that absolute path in single quotes and use forward slashes since it is a literal value.
That's not a cause of the resource itself, that's a cause of your fgetcsv call for the enclosure. This must be a character, you have given it ''. Give it an enclosing character so if you have a , in your field it can still parse correctly.
That's not a cause of the resource itself, that's a cause of your fgetcsv call for the enclosure. This must be a character, you have given it ''. Give it an enclosing character so if you have a , in your field it can still parse correctly.
Enclosure cannot be nothing. You have given it nothing here: fgetcsv($fp, $length, $fildDelineate, ''). Give it a valid character to treat as the enclosure.
Enclosure cannot be nothing. You have given it nothing here: fgetcsv($fp, $length, $fildDelineate, ''). Give it a valid character to treat as the enclosure.
Theoretically it says *character* which implicitly is any character, but no it cannot be a linefeed as that wouldn't make any sense. Delimiters and enclosures should be actual characters, not whitespace based characters.
Theoretically it says *character* which implicitly is any character, but no it cannot be a linefeed as that wouldn't make any sense. Delimiters and enclosures should be actual characters, not whitespace based characters.
Fou-Lu,
I have removed this enclosure even though it doesn't work & even after assigning value it gives me same error can you help me solve this pls?
I have scussesfully coded below code for exporting CSV file in to Mysql but now I want to know how many of the lines ignored while inserting ...that should be a error message to be echoed to the user ..
PHP Code:
<?php session_start(); include_once "C:/xampp/htdocs/SAM/include/database.php"; $myusername = $_SESSION['myusername']; //user who updating
$file = $_FILES['up_file']['tmp_name'];//"new_user_verf.csv"; $fp = fopen($file,"r"); $length = 8096; /// have to be optional length ... $fildDelineate = ','; /// or "|" ... declare what you need $counter = 0; // to omission first row if it is table headers
$importSQL = "INSERT IGNORE INTO $tbl_name0 (`Account_Create_datetime`,`Analyst`,`Acc_User_Name`,`Email_Address`,`User_Ip`) VALUES ('".$date."','".$myusername."','".$value[1]."','".$value[2]."','".$value[3]."')"; $result= mysql_query($importSQL) or die(mysql_error()); $counter++; } fclose($fp); if ($result==1){ $_SESSION['Notify']['type'] = 'Sucess Message:'; $_SESSION['Notify']['msg'] = 'Uploaded total number of->'.$counter.'<-records in to the database.\n'; header("location:/SAM/TEST/test.php") or die("record not inserted"); } elseif($result==IGNORE){ $_SESSION['Notify']['type'] = 'Error Message:'; $_SESSION['Notify']['msg'] = 'Could not upload, number of->'.$counter.'<-records in to the database.\n'; header("location:/mym/new_user/new_user.php") or die("record not inserted"); } else{ $_SESSION['Notify']['type'] = 'Error Message:'; $_SESSION['Notify']['msg'] = 'Some how messed up, check with your admin!!!'; header("location:/SAM/TEST/test.php") or die("record not inserted"); } echo "Imported successfully!"; ?>