vang163 Posted August 5, 2008 Share Posted August 5, 2008 hi, i'm trying to import data from a txt file to a database. But i'm encountering problem. The data was not added and i could not see any messages. Below is my code, pls advise where i have done wrong, thanks. form.php session_start(); <form id="Form1" name="Form1" method="post" action="importdata.php"> <table> <tr> <td> </td> <td><input name="importCSV" type="file"></td> <td> </td> </tr> <tr> <th> </th> <td><input type="submit" name="Submit" value="Import data" size="40"></td> <td> </td> </tr> process the form - importdata.php ... $message = array(); $row = 0; $importFile = $_POST['importCSV']; $handle = fopen("$importFile", "r"); while ($data = fgetcsv($handle, 1000, ",")) { set_time_limit(50); $query = "INSERT into user (`From`, `To`, `owner`, `countryCode`, `country`) VALUES('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[4]."', '".addslashes($data[6])."')"; $result = mysql_query($query) or die("Query failed."); $message[] = "Row " . $row . " data added."; $row++; } fclose ($handle); $message[] = 'Import completed! ' . $row . ' rows added to database.'; $_SESSION['MESSAGE'] = $message; session_write_close(); header("location: form.php"); exit(); ?> Link to comment https://forums.phpfreaks.com/topic/118288-help-with-form/ Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Wait. Do you want the file to be uploaded and then parsed, or do you want the file to be opened and it already exists on the server (i.e: you just specify a filename). Link to comment https://forums.phpfreaks.com/topic/118288-help-with-form/#findComment-608715 Share on other sites More sharing options...
vang163 Posted August 5, 2008 Author Share Posted August 5, 2008 there are existing data in the database. i want to remove all existing data, then import all the data in the text file to the database. no need to upload the file to the server, just import data. Link to comment https://forums.phpfreaks.com/topic/118288-help-with-form/#findComment-608722 Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Then how else do you expect to get the contents of the file? Link to comment https://forums.phpfreaks.com/topic/118288-help-with-form/#findComment-608723 Share on other sites More sharing options...
vang163 Posted August 5, 2008 Author Share Posted August 5, 2008 oh, i see what you mean. yes, the file will have to be upload first in the server. Then import the data. Link to comment https://forums.phpfreaks.com/topic/118288-help-with-form/#findComment-608724 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.