CSV Update
[insert_php]
$servername = “localhost”;
$username = “blueridg_bridge”;
$password = “kr~COSJQ7DP5”;
$dbname = “blueridg_bridge”;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if( mysqli_connect_errno() ){
echo “Failed to connect: “.mysqli_connect_errno();
}else{
$row = 0;
if( ($csv = fopen(“products.csv”, “r”) ) !== FALSE ){
while ( ($data = fgetcsv($csv, 1000, “,”)) !== FALSE ){
$sqlData =””;
$num = count($data);
if($row != 0){
for ($i=0; $i < $num; $i++){
$sqlData = $sqlData."'".$data[$i]."',";
$dataId = $data[0];
}
$sqlData = substr($sqlData,0,-1);
$query = 'SELECT * FROM bridge_products WHERE id = '.$dataId;
$result = mysqli_query($conn, $query);
if($result === FALSE) {
die(mysqli_error());
}else{
$noRows = mysqli_num_rows($result);
if($noRows > 0){
echo ‘Updating ‘.$data[1];
$sql = “UPDATE bridge_products SET name='”.$data[1].”‘, packaging='”.$data[2].”‘, regular_price='”.$data[3].”‘, wholesaler_price='”.$data[4].”‘, distributor_price='”.$data[5].”‘, promoting='”.$data[6].”‘, category='”.$data[7].”‘ WHERE id = “.$dataId;
if(mysqli_query($conn, $sql)){
echo ‘ — Done!
‘;
}
}else{
echo ‘Creating ‘.$data[1];
$sql = “INSERT INTO bridge_products (id, name, packaging, regular_price, wholesaler_price, distributor_price, promoting, category) VALUES (“.$sqlData.”)”;
if(mysqli_query($conn, $sql)){
echo ‘ — Done!
‘;
}
}
}
}
$row++;
echo ‘
‘;
}
fclose($csv);
}
}
[/insert_php]