Forum Moderators: coopster

Message Too Old, No Replies

php import xls into mysql

         

ayushchd

5:39 am on May 9, 2007 (gmt 0)

10+ Year Member



<?php
$target_path = "/home/vol1/example.us/a/ayush/www/virtual/closing/";

$target_path = $target_path . basename( $_FILES['file']['name']);

if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";

include ('mysql.php');
require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('closing/closing.xls');
error_reporting(E_ALL ^ E_NOTICE);

for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)
{

$closing = $data->sheets[0]['cells'][$i][2];

$sql = "UPDATE listedcomp SET closing = '$closing';";
if (mysql_query($sql)) {
echo "Done";
} else {
mysql_error();
}}

} else{
echo "There was an error". $_FILES['file']['error']." uploading the file, please try again!";

}?>

PLEASE HELP! This code isn't importing the data properly....when i see the closing column...all the values are set to 1000, idunno why?

The column is set to interger type......please help

[edited by: eelixduppy at 10:25 am (utc) on May 9, 2007]
[edit reason] example.us [/edit]

omoutop

6:02 am on May 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$sql = "UPDATE listedcomp SET closing = '$closing';";

from this sql, you upodate all the rows in your db, with a single value. I suppose 1000 is the last value in your excel file.

Try to be more specific.
$sql = "UPDATE listedcomp SET closing = '$closing' WHERE {condition} ";.

Else, try to delete al rows and then INSERT the new ones.