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));
Related
I have a problem and I don't know how to solve it. I want to transfer a json to another table and I get a syntax error.
This is my output
INSERT INTO saved_cmd ('id_user','value','store','totalPrice','hour','type_payement') VALUES ('11','"{\"lenght\":0,\"produits\":[{\"id\":29,\"name\":\"Tarte au fraise\",\"count\":1,\"price\":2,\"totalPrice\":2},{\"id\":28,\"name\":\"rose des sables\",\"count\":0,\"price\":2,\"totalPrice\":0}]}"','6','2.00','13:00','caisse')
===================================================
Error: INSERT INTO saved_cmd ('id_user','value','store','totalPrice','hour','type_payement') VALUES ('11','"{\"lenght\":0,\"produits\":[{\"id\":29,\"name\":\"Tarte au fraise\",\"count\":1,\"price\":2,\"totalPrice\":2},{\"id\":28,\"name\":\"rose des sables\",\"count\":0,\"price\":2,\"totalPrice\":0}]}"','6','2.00','13:00','caisse')<br>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id_user','value','store','totalPrice','hour','type_payement') VALUES ('11','"{\' at line 1
I tried to encode my json but i have the same problem
and this is my php
$state = $row['states'];
$id_user = $row['id_user'];
$value = ($row['value']);
$panier = json_encode($value);
$store = $row['store'];
$totalPrice = $row['totalPrice'];
$hour = $row['hour'];
$type_payement = $row['type'];
if ($row['states'] != 4) {
$states = $state + 1;
$sql = "UPDATE cmd SET states = $states WHERE id = '$id'";
if ($conn->query($sql)) {
echo "good:up";
}
} else {
$sql = "INSERT INTO saved_cmd ('id_user','value','store','totalPrice','hour','type_payement') VALUES ('$id_user','$panier','$store','$totalPrice','$hour','$type_payement')";
echo $sql . "\n\n\n===================================================\n\n\n";
if ($conn->query($sql) === true) {
echo "good:save";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
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.
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
}
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);
I need some help with this code:
<?php
include("config.php");
$Servico=implode(" ", $_POST['service']);
$con=mysqli_connect("","","",""); //I hid the info here
if (mysqli_connect_errno())
{
echo "Falhou a conexão ao MySQL: " . mysqli_connect_error();
}
$to = mysqli_query($con,"SELECT Email FROM utilizadores WHERE Nome LIKE '%$Servico%'");
$row = mysqli_fetch_assoc($to);
$row['Email'];
foreach ($row as $destino)
{
echo $destino;
}
?>
The array is created in other code, by selecting one or more users that exists in the database. Then it creates the array with the users selected.
When my array have only a name, it retrieves the email correctly, but when i have more than one, it gives me:
Warning: Invalid argument supplied for foreach() in /home/a5301436/public_html/criartarefa.php on line 49
Can someone help me?
Here's how to do it using IN.
$con=mysqli_connect("","","",""); //I hid the info here
if (mysqli_connect_errno())
{
echo "Falhou a conexão ao MySQL: " . mysqli_connect_error();
}
$Servico=implode(", ", array_map(function($s) use ($con) {
return "'" . mysqli_real_escape_string($con, $s) . "'";
}, $_POST['service']));
$to = mysqli_query($con,"SELECT Email FROM utilizadores WHERE Nome IN ($Servico)");
while ($row = mysqli_fetch_assoc($to)){
echo $row['Email'];
}