Click here to Skip to main content
15,921,841 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to import data from Excel to Mysql database
Here is my code:

C# part:
C#
private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string putanjaExH = tbH.Text;
            string putanjaExE = tbE.Text;
            
           
                if (putanjaExH != "")
                {
                    var wbh = new XLWorkbook(putanjaExH);
                    var wsh = wbh.Worksheet("Sheet1");
                    bool ide = true;
                    int ih = 5;
                    
                        while (ide)
                        {
                            HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://admin.apartman-donat.com/refu.php");
                            ASCIIEncoding encoding = new ASCIIEncoding();
                            string postData = "insert into refh (Gizrade, Investitor, Radni_Zadatak, Predmet_ projekta, Vrsta_projekta_K, Vrsta_projekta_O) values (N'" + wsh.Cell(ih, 1).Value.ToString().Replace(".", "") + "', N'" + wsh.Cell(ih, 2).Value + "', N'" + wsh.Cell(ih, 3).Value + "', N'" + wsh.Cell(ih, 4).Value + "', N'" + wsh.Cell(ih, 5).Value + "', N'" + wsh.Cell(ih, 6).Value + "')";
                            byte[] data = encoding.GetBytes(postData);
                            httpWReq.Method = "POST";
                            httpWReq.ContentLength = data.Length;
                            using (Stream newStream = httpWReq.GetRequestStream())
                            {
                                newStream.Write(data, 0, data.Length);
                            }
                            WebResponse response = httpWReq.GetResponse();
                            // Display the status.
                            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                            // Get the stream containing content returned by the server.
                            Stream dataStream = response.GetResponseStream();
                            // Open the stream using a StreamReader for easy access.
                            StreamReader reader = new StreamReader(dataStream);
                            // Read the content.
                            string responseFromServer = reader.ReadToEnd();
                            // Display the content.
                            MessageBox.Show(responseFromServer);
                            // Clean up the streams.
                            reader.Close();
                            dataStream.Close();
                            response.Close();



                            ih = ih + 1;
                            if (wsh.Cell(ih,1).Value=="")
                            {
                                ide = false;
                            }
                         }
                }
                this.Close();
        }


PHP part:

PHP
<?php
$con = mysql_connect("localhost","test_ref","test");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("refh", $con);

$sql='$_POST[httpWReq]'

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

echo "1 record added";

mysql_close($con);
?>



response is:
lt;br> Parse error</br> : Syntax error: unexpected T_IF in .....

Could someone tell me what am I doing wrong?????
Posted
Updated 26-Apr-15 10:39am
v2
Comments
Sinisa Hajnal 27-Apr-15 6:10am    
Not PHP expert this comment is for little advice: close and dispose your disposable objects in finally block or you'll get memory leaks on exceptions. And you're not handling exceptions here...Sretno.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900