Jump to content

problem with importing CVS to mysql


dizel247

Recommended Posts

Hello,

 

I am trying  a very simple csv import to mysql. When I import the file, it imports blanks. After my search, I realize my page doesn't pass actual path of the file, it passes just file name. When I manually add the path of the file it imports with no problem. Can some one point me to the right direction.

 

Thanks in advance.

 

Roman

 

Code Bellow:

 

page1.php

          <form action="page2.php" method="post" name="form1" id="form1">

            <table border="0" align="center" cellpadding="5" class="nugget">

              <tr>

                <td colspan="2" class="topheader"><div align="center"><span class="lbl">Rate Load Wizard</span></div></td>

                </tr>

              <tr>

                <td>Report Name:</td>

                <td><label>

                <select name="report_name" id="report_name">

                  <option value="">Select Report Name</option>

                  <?php

do { 

?>

                  <option value="<?php echo $row_rsAccountName['report_id']?>"><?php echo $row_rsAccountName['report_name']?></option>

                  <?php

} while ($row_rsAccountName = mysql_fetch_assoc($rsAccountName));

  $rows = mysql_num_rows($rsAccountName);

  if($rows > 0) {

      mysql_data_seek($rsAccountName, 0);

  $row_rsAccountName = mysql_fetch_assoc($rsAccountName);

  }

?>

                </select>

                </label></td>

              </tr>

              <tr>

                <td>Lodaing Data For:</td>

                <td><table width="200">

                    <tr>

                      <td><label>

                        <input type="radio" name="account_type" value="1" id="acc_data_type_0" />

                        Customers</label></td>

                    </tr>

                    <tr>

                      <td><label>

                        <input type="radio" name="account_type" value="2" id="acc_data_type_1" />

                        Vendors</label></td>

                    </tr>

                </table></td>

              </tr>

              <tr>

                <td>Select File to load:</td>

                <td><label>

                  <input name="userfile" type="file" class="sm" id="userfile" />

                  </label>                </td>

              </tr>

              <tr>

                <td> </td>

                <td><label>

                  <div align="right">

                    <input type="submit" name="submit" id="submit" value="submit" />

                    </div>

                </label></td>

              </tr>

            </table>

            </form>

Page2.php

 

<?php

print_r($_POST);

$path = $_POST['userfile'];

$fcontents = file ("$path");

echo $path;

  for($i=0; $i<sizeof($fcontents); $i++) {

  $line = trim($fcontents[$i]);

  $arr = explode(",", $line);

  #if your data is comma separated

  # instead of tab separated,

  # change the '\t' above to ','

  $reportname=$_POST['report_name'];

$account_type=$_POST['account_type'];

$sql = "insert into tb_accounts (carrier_code, minutes, profit, revenue, cost, report_id, account_type) values ('$arr[0]', '$arr[1]', '$arr[2]', '$arr[3]', '$arr[4]', '$reportname', '$account_type')";  

 

  mysql_query($sql);

  echo $sql ."<br>\n";

  if(mysql_error()) {

echo mysql_error() ."<br>\n";

  }

}

 

?>

Debug:

 

Array ( [report_name] => 2 [account_type] => 1 [userfile] => test.csv [submit] => submit ) test.csv

insert into tb_accounts (carrier_code, minutes, profit, revenue, cost, report_id, account_type) values ('', '', '', '', '', '2', '1')

 

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.