tas26
Power Member
- Apr 21, 2009
- 629
- 314
I have a php code to update my database with certain data from a csv file. the issue is i want to post data to my database as this
"Electronics > Arcade Equipment" where as this script coverts it to
"Electronics > Arcade Equipment"
which is not required and breaks everything for me. can someone please add the missing part to the below given code so that the data posted to database remains as i provide in CSV.
here is the code.
"Electronics > Arcade Equipment" where as this script coverts it to
"Electronics > Arcade Equipment"
which is not required and breaks everything for me. can someone please add the missing part to the below given code so that the data posted to database remains as i provide in CSV.
here is the code.
Code:
<?php
include("include/session.php");
function mysql_update_array($table, $data) {
$qr="update ".$table." set ";
foreach ($data as $field=>$value) {
if($field=="sku"){
}
else{
$qr.='`' . $field . '`'."='" . mysql_real_escape_string($value) . "' , ";
}
}
$qr.="where sku='".$data["sku"]."'";
$qr=str_replace(", where","where",$qr);
return $qr;
}
if(isset($_SESSION["db_name"]) && isset($_SESSION["db_user"]) && isset($_SESSION["db_pass"]) && isset($_SESSION["file_path"])){
$db_host="localhost";
extract($_SESSION);
$dblink=mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($db_name,$dblink) or die(mysql_error());
$row = 1;
if(($handle = fopen($file_path, "r")) !== FALSE) {
$data_arr=array();
$heads=array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
if($row==1)
$heads[]=$data[$c];
else{
$data_arr[$heads[$c]]=$data[$c];
}
}
if($row!=1){
$qr=mysql_update_array("product", $data_arr);
mysql_query($qr) or die(mysql_error());
}
$row++;
}
unset($_SESSION["db_name"]);
unset($_SESSION["db_user"]);
unset($_SESSION["db_pass"]);
unset($_SESSION["file_path"]);
header("location:index.php?msg=".base64_encode("Database Updated Successfully."));
die;
}
}
else{
header("location:index.php?err=".base64_encode("No Direct Access."));
die;
}
?>