1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP code encoding issue

Discussion in 'PHP & Perl' started by tas26, Nov 6, 2012.

Thread Status:
Not open for further replies.
  1. tas26

    tas26 Power Member

    Joined:
    Apr 21, 2009
    Messages:
    548
    Likes Received:
    274
    Occupation:
    student
    Location:
    BHW
    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.

    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;    
    }
    ?>
    
     
  2. madoctopus

    madoctopus Supreme Member

    Joined:
    Apr 4, 2010
    Messages:
    1,249
    Likes Received:
    3,498
    Occupation:
    Full time IM
    try this:

    PHP:
    <?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($handle1000",")) !== FALSE) {             $num count($data);             for ($c=0$c $num$c++) {                 if($row==1)                     $heads[]=$data[$c];                 else{                     $data_arr[$heads[$c]]=htmlentities($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;     } ?>
     
    • Thanks Thanks x 1
  3. tas26

    tas26 Power Member

    Joined:
    Apr 21, 2009
    Messages:
    548
    Likes Received:
    274
    Occupation:
    student
    Location:
    BHW
    + REP for ya it works.
     
  4. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    Thread moved and closed as resolved :)
     
Thread Status:
Not open for further replies.