Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 03:34 PM
Newbie
 
Join Date: Dec 2007
Posts: 19
Default upload csv data to database using update query

hi
i am trying to update my sql table data with the csv/excel file data
my sql table is as follows
Expand|Select|Wrap|Line Numbers
  1. test test1
  2. 23   usd
  3. 25   ero
  4. 34   test
  5.  
and my csv file has
Expand|Select|Wrap|Line Numbers
  1. 34 testing
  2. 23 usding
  3.  
so here i have to update 34 and 23 as in the csv file
i am trying with the code as follows

Expand|Select|Wrap|Line Numbers
  1. $link = mysql_connect('localhost', 'root', 'root') or die('Could not
  2. connect: ' . mysql_error());
  3. $handle = fopen("C:/testexcel.csv","r");
  4.  
  5. // loop content of csv file, using comma as delemiter
  6. while (($data = fgetcsv($handle, 4096, ",")) !== FALSE)
  7.  {
  8.     $test= $data[0];
  9.     $test1= $data[1];
  10. //$price2 = floatval($data[2]);
  11.  
  12.     $query = "SELECT * FROM testdb";
  13. //$test1 = mysql_num_rows($query);
  14. //echo $test1;
  15.  
  16.         if (!$result = mysql_query($query))
  17.          {
  18.             continue;
  19.             }
  20.  
  21.     if ($line = mysql_fetch_array($result, MYSQL_ASSOC))
  22.      {
  23. // entry exists update
  24. //$query = "UPDATE testdb SET test='$dist' WHERE test=$test1";
  25.     mysql_query("UPDATE testdb SET test = '$test1' WHERE phoneno= '$phone'");
  26.     if (mysql_affected_rows() <= 0)
  27.      {
  28.         echo "testing sucessfull";
  29.         }
  30.     } 
  31.     else {
  32.         // entry don't exists continue or insert...
  33.  
  34.             }
  35.  
  36. mysql_free_result($result);
  37. }
  38.  
  39. fclose($handle);
  40. mysql_close($link);
  41.  
  42. //error_reporting(0) ;// show no warnings
  43. error_reporting(E_ALL); //show all warnings.
  44.  
  45. ?>
  46.  
and i have tried with the above code
so kindly help me with the solution as it is not updating to the database table
regards
yuvaraj

Last edited by Atli; August 28th, 2008 at 08:57 PM. Reason: Fixed the [code] tags.
Reply
  #2  
Old August 28th, 2008, 05:52 PM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 839
Default

[PHP]mysql_query("UPDATE testdb SET test = '$test1' WHERE phoneno= '$phone'");[/PHP] I don't see a $phone variable
Reply
  #3  
Old August 29th, 2008, 05:46 AM
Newbie
 
Join Date: Dec 2007
Posts: 19
Default

that was my actual code and i have posted my sample with this post kindly reply me with the solution



<?
$link = mysql_connect('localhost', 'root', 'root') or die('Could not
connect: ' . mysql_error());
$handle = fopen("C:/testexcel.csv","r");

// loop content of csv file, using comma as delemiter
while (($data = fgetcsv($handle, 4096, ",")) !== FALSE)
{
$test1= $data[0];
$test2 = $data[1];

$query = "SELECT someF2 FROM mytestdb";
if (!$result = mysql_query($query))
{
continue;
}

if ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
mysql_query("UPDATE mytestdb SET someF1 = '$test1' WHERE someF2= '$test2'");
if (mysql_affected_rows() <= 0)
{
echo "testing sucessfull";
}
}
else {
// entry don't exists continue or insert...
}

mysql_free_result($result);
}

fclose($handle);
mysql_close($link);

error_reporting(E_ALL); //show all warnings.

fclose($handle);

?>


this how i have tried but my database table is not updating so provide me a solution

regards
yuvaraj
Reply
  #4  
Old August 29th, 2008, 08:59 AM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 839
Default

You do not throw a load of code at us and demand an answer.
To find a solution we need to know what is happening.
I have not checked your syntax, I have better things to do, but I suspect the form is not passing the data as you think.
Also, this need to be a the beginning of your script
Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL); //show all warnings.
Not the end.
Please put some echo statements in there that displays the contents of your variables and arrays,
then maybe the problem will become obvious
Reply
  #5  
Old August 29th, 2008, 09:55 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 639
Default

you could try var_dump($query) to see if the MySQL syntax is correct (i.e. all the php variables are replaced (I have my doubts about '$test' because single quoted strings are not parsed))

regards
Reply
  #6  
Old August 29th, 2008, 02:29 PM
Newbie
 
Join Date: Dec 2007
Posts: 19
Default

hi
thanks for ur reply i got it with some other code in php
regards
yuvaraj
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles