0

this code is working perfect but when i add two row its not working

please help me to fix this issue thanks

not working

  mysql_query("INSERT INTO sale2 (username, date, item, quantity, amount) VALUES

this is working

 mysql_query("INSERT INTO sale2 (username, date, item) VALUES 

and this is complete code

<?php  

            //connect to the database 
            $connect = mysql_connect("localhost","root",""); 
            mysql_select_db("member",$connect); //select the table 
            // 

            if ($_FILES[csv][size] > 0) { 

                //get the csv file 
                $file = $_FILES[csv][tmp_name]; 
                $handle = fopen($file,"r"); 

                //loop through the csv file and insert into database 
                do { 
                    if ($data[0]) { 
                        mysql_query("INSERT INTO sale2 (username, date, item) VALUES 
                            ( 
                                '".addslashes($data[0])."', 
                                '".addslashes($data[1])."', 
                                '".addslashes($data[2])."' 
            \

                            ) 
                        "); 
                    } 
                }  while ($data = fgetcsv($handle,1000,",","'")); 
                // 

                //redirect 
                header('Location: csv.php?success=1'); die; 

            } 

            ?> 

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
            <html xmlns="http://www.w3.org/1999/xhtml"> 
            <head> 
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
            <title>Import a CSV File with PHP & MySQL</title> 
            </head> 

            <body> 

            <?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?> 

            <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
              Choose your file: <br /> 
              <input name="csv" type="file" id="csv" /> 
              <input type="submit" name="Submit" value="Submit" /> 
            </form> 

            </body> 
            </html> 
7
  • 1
    What errors are you getting when you mean not working? Commented Nov 8, 2013 at 7:27
  • no error show in page Commented Nov 8, 2013 at 7:29
  • add error_reporting(E_ALL); at top of file Commented Nov 8, 2013 at 7:30
  • Is it getting redirected to the location you mentioned? Commented Nov 8, 2013 at 7:32
  • Consider to post sample data from your file and exact table schema Commented Nov 8, 2013 at 7:34

4 Answers 4

1
mysql_query("INSERT INTO `sale2` (`username`, `date`, `item`, `quantity`, `amount`) VALUES 
                        (
                            '".trim($data[0], '"')."',
                            '".trim($data[1], '"')."', 
                            '".trim($data[2], '"')."',
                            '".trim($data[3], '"')."'
                        )
                    ");
Sign up to request clarification or add additional context in comments.

Comments

0

Try to find the error where your script is not working. Also use exception handling try-catch action and get message through $e->getMessage();

try {
   mysql_query("INSERT INTO sale2 (username, date, item) VALUES 
                        ( 
                            '".addslashes($data[0])."', 
                            '".addslashes($data[1])."', 
                            '".addslashes($data[2])."' 
        \

                        ) 
                    "); 
} catch (Exception $e) {
 echo $e->getMessage();
}

1 Comment

dear i replace ur code no error found and it is showing this text but it is not upload Your file has been imported.
0

First Create the database below is code.

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `contacts`;
CREATE TABLE `contacts` (
  `contact_id` int(11) NOT NULL auto_increment,
  `contact_first` varchar(255) character set latin1 default NULL,
  `contact_last` varchar(255) character set latin1 default NULL,
  `contact_email` varchar(255) character set latin1 default NULL,
  PRIMARY KEY  (`contact_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Second step created the csv file please have a look.

contacts.csv

Jim,Smith,[email protected]
Joe,Tester,[email protected]

Last step connect the database and run the import csv file below is the code.

 <?php  
$connect = mysql_connect("localhost","root",""); 
mysql_select_db("mydatabase",$connect);


if ($_FILES[csv][size] > 0) { 

    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r"); 

    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."' 
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",","'")); 

    header('Location: index.php?success=1'); die; 

} 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Import a CSV File with PHP & MySQL</title> 
</head> 

<body> 

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } ?> 

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
  Choose your file: <br /> 
  <input name="csv" type="file" id="csv" /> 
  <input type="submit" name="Submit" value="Submit" /> 
</form> 

</body> 
</html> 

Comments

0

I think you need to convert CSV file in collection first. After that you can add loop and call insert queries to insert data.

Here you will get code to convert CSV file into collection array. best-way-to-upload-and-read-csv-file-in-php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.