423,850 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,850 IT Pros & Developers. It's quick & easy.

upload csv data to database using update query

P: 19
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
Aug 28 '08 #1
Share this Question
Share on Google+
5 Replies


code green
Expert 100+
P: 1,726
[PHP]mysql_query("UPDATE testdb SET test = '$test1' WHERE phoneno= '$phone'");[/PHP] I don't see a $phone variable
Aug 28 '08 #2

P: 19
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
Aug 29 '08 #3

code green
Expert 100+
P: 1,726
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
Aug 29 '08 #4

Dormilich
Expert Mod 5K+
P: 8,639
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
Aug 29 '08 #5

P: 19
hi
thanks for ur reply i got it with some other code in php
regards
yuvaraj
Aug 29 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.