weird chars when data is inserted in mysql

Discussion in 'PHP' started by choy, Sep 26, 2007.

  1. #1
    hello! i have a big trouble inserting data in mysql

    i have a method called InsertData


    public function InsertData($keys ,$values,$table)
    {

    $this->init(); //initialize the conn and stuff
    $valueStr="";

    //query construction

    $sql = "INSERT INTO $table (";

    foreach ($keys as $key) {
    $valueStr.= $key .",";
    }

    $valueStr = substr($valueStr,0,strlen($valueStr)-1);
    $sql .= $valueStr . ") VALUES (";
    $valueStr ="";

    foreach ($values as $value) {

    if (!is_numeric($value))
    {
    $value = "'" .$value ."'";
    }
    $valueStr.= $value.",";
    }

    $valueStr = substr($valueStr,0,strlen($valueStr)-1);
    $sql .= $valueStr . ")" ;


    //echo $sql; echo mysql_error();
    $success = mysql_query($sql, $this->conn) or $success=0 ;

    return $success;
    }

    if i try to insert some data like "Adrián"... the query insert other characters (like "adrían34")

    i called the function this way




    $keys[] ="name";
    $keys[] = "descripction";
    $values [] = $_GET["txtName"];
    $values [] = $_GET["txtDesc"];




    $ob->InsertData($keys ,$values,"division");





    i thought that the problem was the array that i passed to the function with the data.... BUT when i print the query it appears like this


    "INSERT INTO division (name,description) VALUES ('adrián','adrián') "

    and then... the query are executed and the value is all chunk

    i tried to change the columns encodings and the headers but nothings work...

    please help me im so desperate



    choy
     
    choy, Sep 26, 2007 IP
  2. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try doing a htmlentities on the data before inserting it. That will make all weird characters into htmlentities.
     
    tamen, Sep 26, 2007 IP