Inserting CSV to MySQL












0















I would like to insert a CSV to MySQL. How can I only update my table if the column sha1 is existing in MySQL table? If the column sha1 was exist, insert it.



I have this code to insert csv:



while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}









share|improve this question




















  • 1





    Don't format SQL with parameters, use a prepared statement and pass in the parameters, this will take care of escaping and formatting the parameters correctly.

    – SPlatten
    Nov 21 '18 at 10:16
















0















I would like to insert a CSV to MySQL. How can I only update my table if the column sha1 is existing in MySQL table? If the column sha1 was exist, insert it.



I have this code to insert csv:



while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}









share|improve this question




















  • 1





    Don't format SQL with parameters, use a prepared statement and pass in the parameters, this will take care of escaping and formatting the parameters correctly.

    – SPlatten
    Nov 21 '18 at 10:16














0












0








0








I would like to insert a CSV to MySQL. How can I only update my table if the column sha1 is existing in MySQL table? If the column sha1 was exist, insert it.



I have this code to insert csv:



while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}









share|improve this question
















I would like to insert a CSV to MySQL. How can I only update my table if the column sha1 is existing in MySQL table? If the column sha1 was exist, insert it.



I have this code to insert csv:



while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}






php mysql csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:13









kit

1,1063816




1,1063816










asked Nov 21 '18 at 10:03









GodshandGodshand

989




989








  • 1





    Don't format SQL with parameters, use a prepared statement and pass in the parameters, this will take care of escaping and formatting the parameters correctly.

    – SPlatten
    Nov 21 '18 at 10:16














  • 1





    Don't format SQL with parameters, use a prepared statement and pass in the parameters, this will take care of escaping and formatting the parameters correctly.

    – SPlatten
    Nov 21 '18 at 10:16








1




1





Don't format SQL with parameters, use a prepared statement and pass in the parameters, this will take care of escaping and formatting the parameters correctly.

– SPlatten
Nov 21 '18 at 10:16





Don't format SQL with parameters, use a prepared statement and pass in the parameters, this will take care of escaping and formatting the parameters correctly.

– SPlatten
Nov 21 '18 at 10:16












2 Answers
2






active

oldest

votes


















1














Check this, This may help you



<?php
while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

// Get record where sha1
$check_sha = "SELECT sha1 FROM jeremy_table_trend WHERE sha1='".$sha1."'";
$check_shaquery = mysqli_query($con , $check_sha);
if($check_shaquery){
$sha_count = mysqli_num_rows($check_shaquery);
}

// Check if sha1 already in database
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_trend` SET `date_sourced`='".$date."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}
?>





share|improve this answer
























  • Please be aware that this code is prone to SQL injections and very bad coding style all around.

    – YetiCGN
    Nov 21 '18 at 11:06



















1














Please, this is 2018. No need for procedural low-level code anymore. Take a look at a library like Spout to read CSV and use PDO for object-oriented access to the database.



I assume the column sha1 has a UNIQUE index set? If not, it should. Then you can do this in one neat SQL statement with the INSERT ... ON DUPLICATE KEY UPDATE syntax:



$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes') ON DUPLICATE KEY UPDATE";


In addition to checking for the existence of an entry with the same hash in the database, you could also use an ORM layer that will take care of this for you.






share|improve this answer
























  • keeps on giving error, is this the full format?

    – Godshand
    Nov 22 '18 at 6:02











  • What's the error message? Did you set the UNIQUE index on the column "sha1"? It needs to have the index for the query to work.

    – YetiCGN
    Nov 23 '18 at 14:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409552%2finserting-csv-to-mysql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Check this, This may help you



<?php
while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

// Get record where sha1
$check_sha = "SELECT sha1 FROM jeremy_table_trend WHERE sha1='".$sha1."'";
$check_shaquery = mysqli_query($con , $check_sha);
if($check_shaquery){
$sha_count = mysqli_num_rows($check_shaquery);
}

// Check if sha1 already in database
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_trend` SET `date_sourced`='".$date."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}
?>





share|improve this answer
























  • Please be aware that this code is prone to SQL injections and very bad coding style all around.

    – YetiCGN
    Nov 21 '18 at 11:06
















1














Check this, This may help you



<?php
while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

// Get record where sha1
$check_sha = "SELECT sha1 FROM jeremy_table_trend WHERE sha1='".$sha1."'";
$check_shaquery = mysqli_query($con , $check_sha);
if($check_shaquery){
$sha_count = mysqli_num_rows($check_shaquery);
}

// Check if sha1 already in database
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_trend` SET `date_sourced`='".$date."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}
?>





share|improve this answer
























  • Please be aware that this code is prone to SQL injections and very bad coding style all around.

    – YetiCGN
    Nov 21 '18 at 11:06














1












1








1







Check this, This may help you



<?php
while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

// Get record where sha1
$check_sha = "SELECT sha1 FROM jeremy_table_trend WHERE sha1='".$sha1."'";
$check_shaquery = mysqli_query($con , $check_sha);
if($check_shaquery){
$sha_count = mysqli_num_rows($check_shaquery);
}

// Check if sha1 already in database
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_trend` SET `date_sourced`='".$date."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}
?>





share|improve this answer













Check this, This may help you



<?php
while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

// Get record where sha1
$check_sha = "SELECT sha1 FROM jeremy_table_trend WHERE sha1='".$sha1."'";
$check_shaquery = mysqli_query($con , $check_sha);
if($check_shaquery){
$sha_count = mysqli_num_rows($check_shaquery);
}

// Check if sha1 already in database
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_trend` SET `date_sourced`='".$date."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}
?>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 10:51









F5 BuddyF5 Buddy

3584




3584













  • Please be aware that this code is prone to SQL injections and very bad coding style all around.

    – YetiCGN
    Nov 21 '18 at 11:06



















  • Please be aware that this code is prone to SQL injections and very bad coding style all around.

    – YetiCGN
    Nov 21 '18 at 11:06

















Please be aware that this code is prone to SQL injections and very bad coding style all around.

– YetiCGN
Nov 21 '18 at 11:06





Please be aware that this code is prone to SQL injections and very bad coding style all around.

– YetiCGN
Nov 21 '18 at 11:06













1














Please, this is 2018. No need for procedural low-level code anymore. Take a look at a library like Spout to read CSV and use PDO for object-oriented access to the database.



I assume the column sha1 has a UNIQUE index set? If not, it should. Then you can do this in one neat SQL statement with the INSERT ... ON DUPLICATE KEY UPDATE syntax:



$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes') ON DUPLICATE KEY UPDATE";


In addition to checking for the existence of an entry with the same hash in the database, you could also use an ORM layer that will take care of this for you.






share|improve this answer
























  • keeps on giving error, is this the full format?

    – Godshand
    Nov 22 '18 at 6:02











  • What's the error message? Did you set the UNIQUE index on the column "sha1"? It needs to have the index for the query to work.

    – YetiCGN
    Nov 23 '18 at 14:52
















1














Please, this is 2018. No need for procedural low-level code anymore. Take a look at a library like Spout to read CSV and use PDO for object-oriented access to the database.



I assume the column sha1 has a UNIQUE index set? If not, it should. Then you can do this in one neat SQL statement with the INSERT ... ON DUPLICATE KEY UPDATE syntax:



$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes') ON DUPLICATE KEY UPDATE";


In addition to checking for the existence of an entry with the same hash in the database, you could also use an ORM layer that will take care of this for you.






share|improve this answer
























  • keeps on giving error, is this the full format?

    – Godshand
    Nov 22 '18 at 6:02











  • What's the error message? Did you set the UNIQUE index on the column "sha1"? It needs to have the index for the query to work.

    – YetiCGN
    Nov 23 '18 at 14:52














1












1








1







Please, this is 2018. No need for procedural low-level code anymore. Take a look at a library like Spout to read CSV and use PDO for object-oriented access to the database.



I assume the column sha1 has a UNIQUE index set? If not, it should. Then you can do this in one neat SQL statement with the INSERT ... ON DUPLICATE KEY UPDATE syntax:



$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes') ON DUPLICATE KEY UPDATE";


In addition to checking for the existence of an entry with the same hash in the database, you could also use an ORM layer that will take care of this for you.






share|improve this answer













Please, this is 2018. No need for procedural low-level code anymore. Take a look at a library like Spout to read CSV and use PDO for object-oriented access to the database.



I assume the column sha1 has a UNIQUE index set? If not, it should. Then you can do this in one neat SQL statement with the INSERT ... ON DUPLICATE KEY UPDATE syntax:



$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes') ON DUPLICATE KEY UPDATE";


In addition to checking for the existence of an entry with the same hash in the database, you could also use an ORM layer that will take care of this for you.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 11:05









YetiCGNYetiCGN

53337




53337













  • keeps on giving error, is this the full format?

    – Godshand
    Nov 22 '18 at 6:02











  • What's the error message? Did you set the UNIQUE index on the column "sha1"? It needs to have the index for the query to work.

    – YetiCGN
    Nov 23 '18 at 14:52



















  • keeps on giving error, is this the full format?

    – Godshand
    Nov 22 '18 at 6:02











  • What's the error message? Did you set the UNIQUE index on the column "sha1"? It needs to have the index for the query to work.

    – YetiCGN
    Nov 23 '18 at 14:52

















keeps on giving error, is this the full format?

– Godshand
Nov 22 '18 at 6:02





keeps on giving error, is this the full format?

– Godshand
Nov 22 '18 at 6:02













What's the error message? Did you set the UNIQUE index on the column "sha1"? It needs to have the index for the query to work.

– YetiCGN
Nov 23 '18 at 14:52





What's the error message? Did you set the UNIQUE index on the column "sha1"? It needs to have the index for the query to work.

– YetiCGN
Nov 23 '18 at 14:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409552%2finserting-csv-to-mysql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown