Undefined offset error on php when importing a CSV - php

I am trying to import a CSV file into my database and it is being successfully executed at the end. But, during the execution, I receive an "undefined offset" error message and when I checked the data imported, I see that there are some null records updated in the table. How can I avert importing these null cells into my database? I also would like not to see these error messages.
<?php
require_once("database.php");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/// Delete table contents
$dsql = "TRUNCATE TABLE User_Mirror_Tbl";
if ($conn->query($dsql) === TRUE) {
echo "Table content is truncated successfully". PHP_EOL;
} else {
echo "Error: " . $dsql . "<br>" . $conn->error;
}
//read file
$csvfile=file_get_contents("/samba/import/User_Update_Tbl.csv");
//counters:
$record_number=0;
$record_number_err=0;
$lines = explode(PHP_EOL, $csvfile);
$array = array();
foreach ($lines as $line) {
$field = str_getcsv($line);
if $field[0] != ''){
$field[1]= ( $field[1] == '' ? NULL : $field[1]);
$field[6]= ( $field[6] == '' ? NULL : $field[6]);
$field[7]= ( $field[7] == '' ? NULL : $field[7]);
$sql="INSERT INTO User_Mirror_Tbl (History_Record_ID, Employee_ID, Application_ID, User_Status, Record_Date, User_Name, User_Role, Last_Signon, UserKeyString)
VALUES
('$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]') ";
//insert record to database
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully". PHP_EOL;
$record_number=$record_number+1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
$record_number_err=$record_number_err+1;
}
}
}
echo $record_number.' Successful record and '.$record_number_err.' Unsuccessful record executed.';
$conn->close();
<?php
require_once("database.php");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/// Delete table contents
$dsql = "TRUNCATE TABLE User_Mirror_Tbl";
if ($conn->query($dsql) === TRUE) {
echo "Table content is truncated successfully". PHP_EOL;
} else {
echo "Error: " . $dsql . "<br>" . $conn->error;
}
//read file
$csvfile=file_get_contents("/samba/import/User_Update_Tbl.csv");
//counters:
$record_number=0;
$record_number_err=0;
$lines = explode(PHP_EOL, $csvfile);
$array = array();
foreach ($lines as $line) {
$field = str_getcsv($line);
if $field[0] != ''){
$field[1]= ( $field[1] == '' ? NULL : $field[1]);
$field[6]= ( $field[6] == '' ? NULL : $field[6]);
$field[7]= ( $field[7] == '' ? NULL : $field[7]);
$sql="INSERT INTO User_Mirror_Tbl (History_Record_ID, Employee_ID, Application_ID, User_Status, Record_Date, User_Name, User_Role, Last_Signon, UserKeyString)
VALUES
('$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]') ";
//insert record to database
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully". PHP_EOL;
$record_number=$record_number+1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
$record_number_err=$record_number_err+1;
}
}
}
echo $record_number.' Successful record and '.$record_number_err.' Unsuccessful record executed.';
$conn->close();

Sometimes you need to ignore the last line or lines of the csv. I set the number of ignore lines from the top as $start_offset and the number of lines to ignore from the bottom as $end_offset. Start with zero and increase until the offset error goes awayHere's how I do it:
$data = file_get_contents($filename);//load up csv
$data_array = explode("\n", $data);//break file into lines
$csv = array_map('str_getcsv', $data_array);//break up comma delimited
$csv_len = count($csv); //count of number of lines
$start_offset = 2;
$end_offset = 3;
for ($i=$start_offset; $i<$csv_len-$end_offset; $i++)
{
//access columns as $csv[$i][0], $csv[$i][1] etc
}

Related

Using explode arrays in foreach and only last array Inserted into table, how does arrays work?

I get a text from html-form, didnt mention it here, but it looks like:
John:John
Mike:Mike
Root:Admin
Here is my php code:
$text = explode("\n", $_POST["info"]);
// - get data from html form and //explode it to pieces
print_r($text);
// result is: Array ( [0] => John:John [1] => Mike:Mike [2] => Root:Admin )
foreach ($text as $key => $value) {
$val = explode (":", $value);
// want to explode it to pieces, result must be 0=>John 1=>John, 0=>Mike 1=>Mike, [0]=>Root [1]=>Admin
$sql = "INSERT INTO `redtable`(`NAME`,`NAME2`) VALUES('$val[0]','$val[1]');";
}
When this code runs, it inserts into database only the last line, which are (Root:Admin), why it doesn't inserts John:John, Mike:Mike ...?
Where is the mistake?
Here is the result of echo $sql:
INSERT INTO `redtable`(`IGNAME`,`IGPASS`) VALUES('John','John ');INSERT INTO `redtable`(`IGNAME`,`IGPASS`) VALUES('Mike','Mike ');INSERT INTO `redtable`(`IGNAME`,`IGPASS`) VALUES('Root','Admin');
Here is the full code:
<?php
$servername = "localhost";
$username = "mysql";
$password = "mysql";
$dbname = "red";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$pieces = explode("\n", $_POST["info"]);
foreach ($pieces as $key => $value) {
$val = explode (":", $value);
$sql = "INSERT INTO `redtable`(`IGNAME`,`IGPASS`) VALUES('$val[0]','$val[1]');";
echo $sql;
}
if ($conn->query($sql) === TRUE) {
echo "Days left updated";
} else {
mysqli_error($conn);
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Have trying using if-else statements, like this:
# code...
$val = explode (":", $value);
# print_r($val);
if (1 == 1) {
$sql = "INSERT INTO `redtable`(`IGNAME`,`IGPASS`) VALUES('$val[0]','$val[1]');";
}
else {
echo "esle";
}
}
The same result, only the last line have been inserted to the DB.
GUYS, If SOMEONE NEED THE WORKING SOLUTION WATCH #Matt Rabe answer - working like a charm, you need just replace the brackets!
Mark Baker is right - you are executing your sql outside of your foreach loop. You are defining the $sql var inside your foreach, but the actual execution of it ($conn->query($sql)) occurs outside of the foreach.
Change this:
foreach ($pieces as $key => $value) {
$val = explode (":", $value);
$sql = "INSERT INTO `redtable`(`IGNAME`,`IGPASS`) VALUES('$val[0]','$val[1]');";
echo $sql;
}
if ($conn->query($sql) === TRUE) {
echo "Days left updated";
} else {
mysqli_error($conn);
echo "Error: " . $sql . "<br>" . $conn->error;
}
To this:
foreach ($pieces as $key => $value) {
$val = explode (":", $value);
$sql = "INSERT INTO `redtable`(`IGNAME`,`IGPASS`) VALUES('$val[0]','$val[1]');";
echo $sql;
if ($conn->query($sql) === TRUE) {
echo "Days left updated";
} else {
mysqli_error($conn);
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
Your code has numerous issues beyond this, but this should address your stated question.

How can I add each result to my table in MySQL

I have a php file (with simple_HTML_Dom) which scrape all the URLs of a CSV file.
He extract all the info I need, and all OK, but now I want to add all these results on my MySQL table.
I want each result adds in one row in MySQL table.
That's the code I have so far:
<?php
require 'libs/simple_html_dom/simple_html_dom.php';
set_time_limit(0);
function scrapUrl($url)
{
$html = new simple_html_dom();
$html->load_file($url);
$names = $html->find('h1');
$manufacturers = $html->find('h2');
foreach ($names as $name) {
echo $name->innertext;
echo '<br>';
}
foreach ($manufacturers as $manufacturer) {
echo $manufacturer->innertext;
echo '<br>';
echo '<hr><br>';
}
}
$rutaCSV = 'csv/urls1.csv'; // Ruta del csv.
$csv = array_map('str_getcsv', file($rutaCSV));
//print_r($csv); // VerĂ¡s que es un array donde cada elemento es array con una de las url.
foreach ($csv as $linea) {
$url = $linea[0];
scrapUrl($url);
}
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($csv as $linea) {
$sql = "INSERT INTO productos (nombre, nombreFabricante) VALUES($name, $manufacturer)";
print ("<p> $sql </p>");
if ($conn->query($sql) === TRUE) {
echo "Items added to the database!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
EDIT: I have updated the code. I have added also the price variable.
The error shown in the output is this one:
Notice: Undefined variable: name in C:\xampp\htdocs\bootstrap\csv2.php on line 69
Notice: Undefined variable: manufacturer in C:\xampp\htdocs\bootstrap\csv2.php on line 69
Error: INSERT INTO productos (nombre, nombreFabricante) VALUES(,)
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' 'https://url.com/es/product1.html', )' at line 1
How can I solve the error for the Notice: Undefined variable: name & price?
I want to add the $name and $manufacturer variables which are inside the function, to my MySQL table
I'm not sure where you define $name so you'll need to check that it's valid.
foreach ($csv as $linea) {
$sql = "INSERT INTO productos (name, manufacturer) VALUES('$name', '{$linea[0]}')";
print ("<p> $sql </p>");
if ($conn->query($sql) === TRUE) {
echo "Items added to the database!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
Try this code , Hope you are collecting each value separately but your need is to insert each set of result in the mysql table as a new row , try the below but it's a rough one make it as per your need.
require 'libs/simple_html_dom/simple_html_dom.php';
set_time_limit(0);
function scrapUrl($url)
{
$ar_name =array();
$ar_man = array();
$html = new simple_html_dom();
$html->load_file($url);
$names = $html->find('h1');
$manufacturers = $html->find('h2');
foreach ($names as $name) {
$ar_name[] = $name->innertext;
}
foreach ($manufacturers as $manufacturer) {
$ar_man[] = $manufacturer->innertext;
}
for($i=o;$i<sizeof($ar_name);$i++)
{
$sql = "INSERT INTO table(name, manufacturer)
VALUES ($ar_name[$i],$ar_man[$i])";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
//echo $url;
}
$rutaCSV = 'csv/urls1.csv'; // Ruta del csv.
$csv = array_map('str_getcsv', file($rutaCSV));

String to Integer Returning 0

I am converting , through type cast , through intval() and other procedures , but what is returned from $Id (string :eg "99973132") is always 0. I will be grateful if anyone can guide me.The value that I am converting from string to int is of length 99973132(all the values are close to this) . I have crossed checked . The value returned in $Id is 99973132 but conversion results in 0
<?php
require_once('../SchemaBuilder/Dbconfig.php');
require_once('../SchemaBuilder/NativeConfiguration.php');
$servername = MYSERVERNAME;
$username = MYUSERNAME;
$password = MYPW;
$dbname = MYDBNAME;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(!file_exists($_FILES['csvdata']['tmp_name']) || !is_uploaded_file($_FILES['csvdata']['tmp_name']))
{ echo "no file";
echo "<br> No file Selected press back to try again!!<br>";
}
else
{
$content= file_get_contents($_FILES["csvdata"]["tmp_name"]);
$content= file_get_contents($_FILES["csvdata"]["tmp_name"]);
$lines = explode("\n", $content);
$i = 0;//initialize
foreach($lines as $value)
{
if($i != 0)
{
$cols[$i] = explode("\t", $value);
if(isset($cols[$i][9]))
{ $id=$cols[$i][0] +0;
$query = "INSERT INTO csvdata(Id,UserName)
VALUES(".$i.", '".$cols[$i][9]. "')" ;
if ($conn->query($query) === TRUE)
{
echo "New record created successfully".'<br>';
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error.'<br>';
}
$x++;
}
}
$i++;
}
$conn->close();
}
?>
Edit : I am loading a .tsv file , after that parsing each line by skiping first line , then exploding by \t . fetching first attribute which is in $id and converting it to integer . but its not converted and gives 0

Compare sql table with txt file line by line with table if variable dosent exsit insert it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
<?php
function db_query()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "single4thenight";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, alias, type, parent, ordering, published FROM iutca_jomcl_locations"; //selects locations from
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " alias" . $row["alias"]. " type: " . $row["type"]. " - parent: " . $row["parent"]. " ordering " . $row["ordering"]. "published " . $row["published"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
}
function read_location()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "single4thenight";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name FROM iutca_jomcl_locations"; //selects locations from
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc()) {
// echo "Location : " . $row["name"]."<br>";
$row_name = $row["name"];
echo $row_name.'<br />';
}
}
$file1 = "./location.txt";
$lines = file($file1);
foreach($lines as $line_num => $line)
{
echo $line;
}
}
My location.txt file contains this
Auburn
Birmingham
Dothan
Gadsden
Huntsville
Mobile
Montgomery
Muscle Shoals
Tuscaloosa
I would like to compare my sql database with txt file to make sure that i do not arealdy have variables inside. I do not want to put duplicates in side my sql i would like to know what is the easiest way to update my sql
You could use INSERT IGNORE INTO instead of just INSERT INTO and MySQL will then ignore duplicate entries. See the MySQL documentation for INSERT for more information. So, based on what I see in your question, your SQL would look something like:
INSERT IGNORE INTO iutca_jomcl_locations ('name') values (?)
Hope this helps! :)
First we read file content to the $content variable
$content = file('mytxt.txt')
As you posted, your file contains words separated with space (if not, skip this and make variable $words contain values you need) so we need to split content, to get each word as array item
$words = explode(" ", $content);
Finally, inserting value and checking if there is one like that existing in DB
foreach($words as $word)
{
$sql = "INSERT iutca_jomcl_locations (name)
SELECT $word
WHERE NOT EXISTS
( SELECT 1
FROM tblSoftwareTitles
WHERE name = $word
);"
$result = $conn->query($sql);
}
iutca_jomcl_locations - table name
name - column to insert (also checking for unique values using this column)
I used This Code and it Worked for me
foreach($lines as $line_num => $line)
{
$line = $line;
//echo $line;
$sql = "SELECT ordering, name FROM iutca_jomcl_locations WHERE name='$line'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
echo "Found Locations: " . $row["name"]." The ordering Number Is " . $row["ordering"]."<br>";
$ordering =$row["ordering"];
}
} else {
if($ordering >=0)
{
$count = $ordering;
//echo "0 results";
$lowerCase = strtolower($line);
$sql = "INSERT INTO iutca_jomcl_locations (name, alias , parent, published,ordering)
VALUES ('$line','$lowerCase','$parent','1','$count')";
$count = $count + 1;
if ($conn->query($sql) === TRUE) {
echo "New record created successfully <br />";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
}

Insert data to database from CSV file. Only inserts last row

I read in data from a csv file, and want to insert it into my database. The issue is that it only inserts the last row in the CSV file each time. However when i print my $sq1 to screen it shows all 48 inserts with the different values that they should have. Could anyone tell me why it only inserts one row into the database?
<?php
// Did modify login values for privacy
$servername = "10.100.";
$username = "myusername";
$password = "abc123";
$dbname = "informationdata";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$file = fopen("ALMGrade.csv","r");
while(! feof($file)){
$ar =fgetcsv($file);
$sql = "INSERT INTO gradetable_copy (Grade, Grade1, Grade2, Grade3, Grade4, Grade5, Grade6)
VALUES ('$ar[0]', '$ar[1]', '$ar[2]', '$ar[3]', '$ar[4]', '$ar[5]', '$ar[6]' )";
echo $sql;
echo "<br>";
}
fclose($file);
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
move the execution of query ($conn->query($sql) ) inside while:
while(! feof($file))
{
$ar =fgetcsv($file);
$sql = "INSERT INTO gradetable_copy (Grade, Grade1, Grade2, Grade3, Grade4, Grade5, Grade6)
VALUES ('$ar[0]', '$ar[1]', '$ar[2]', '$ar[3]', '$ar[4]', '$ar[5]', '$ar[6]' )";
echo $sql;
echo "<br>";
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
fclose($file);

Categories