Search the Community
Showing results for tags 'csv upload mysqli'.
-
Hi peeps I have been using mysql commands for most of my php coding, and i've been told it would be wise to move to mysqli im trying to create a csv upload to database page but its just adding blank fields can someone tell me where im going wrong? as my code wont tell me <?php include('header.php'); ?> <!DOCTYPE html> <head> <title>Upload New Numbers files to the database</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <form action="importData.php" method="post" enctype="multipart/form-data"> <input type="file" name="uploaded_file"><br> <input type="submit" value="Upload file"> </form> </body> </html> <?php error_reporting(E_ALL); ini_set('display_errors', 1); if (isset($_FILES['uploaded_file'])) { //Make sure the file was sent without errors if ($_FILES['uploaded_file']['error'] == 0) { //connect to the database $mysqli = new mysqli ('localhost','root','','PDNumbers'); if (mysqli_connect_errno()) { die("Mysqli connection failed: ". mysqli_connect_error()); } //gather all the required data from the CSV $file= $_FILES['uploaded_file']['tmp_name']; $handle = fopen($file, "r"); $row= 1; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($row == 1) { //skip the first row } else { //csv format like this //$data[0] = mobile number //$data[1] = landline //$data[2] = title //$data[3] = forename //$data[4] = surname //$data[5] = Address1 //$data[6] = Address2 //$data[7] = Address3 //$data[8] = blank field //$data[9] = Town //$data[10] = County //$data[11] = postcode } } } //$import_query = "INSERT IGNORE INTO LepusData (mobile,landline,title,forename,surname,Address1,Address2,Address3,Town,County,postcode) //VALUES ( //'".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."','".$data[5]."','".$data[6]."','".$data[7]."','".$data[8]."','".$data[10]."','".$data[11]."' //)"; $import_query = "INSERT INTO LepusData(mobile) VALUES ('".$data[0]."')"; $result = $mysqli->query($import_query); if ($result) { echo "Success! The file was added into the database"; } else { echo "Error! failed to insert the file"; } // close the mysqli connection $mysqli->close(); } else { echo "Error! The file was not sent!"; } ?> any help is greatly appreciated thanks oh forgot to add, I am ignoring data[9] on purpose, its a blank field