POO PHP function insert into MYSQL [closed]

1

Hello, I want to make a function to insert the data into the table, it is not returning anything, just look at the code:

 function insertInto ($tabela, $colunas=array(), $valores=array()) {

            foreach ( $valores as $valor ){
                   $valor = implode(',', $valores);
                   return $valor;
    }

    foreach ($colunas as $coluna){
                $coluna = implode(',', $colunas);
                return $coluna;
    }

    $sql = "INSERT INTO $tabela ($coluna) VALUES ('$valor')";
            $handle = mysql_query($sql, $conexaobd) or die(mysql_error());

    if(!$this->query($sql, $coluna)->error()){
        return true;
    }

    return false;

}

this is the data code to insert into the table;

             if ($_POST['status_atual'] != $_POST['status']) 
                            {

                                $tabela = 'editais_status_historico';

                                $colunas = 'id_edital';
                                $colunas .= 'login_usuario';
                                $colunas .= 'data';
                                $colunas .= 'estado_anterior';
                                $colunas .= 'estado_novo';

                                $valores = $_POST['status'];
                                $valores .= $_POST['status_atual'];
                                $valores .= $_POST['idedital'];
                                $valores .= $_SESSION['user'];
                                $valores .= date("Y-m-d") . ' ' . date("H:i:s");


                                $status = $_POST['status'];
                                $status_atual = $_POST['status_atual'];

                                $id_edital = '0';
                                $id_usuario = $_SESSION['user'];

                                $data = date("Y-m-d") . ' ';
                                $data .= date("H:i:s");


                                insertInto($tabela, $colunas, $valores);

                            }
    
asked by anonymous 23.03.2015 / 13:49

1 answer

2

For your case it would be something +/- like this:

if ($_POST['status_atual'] != $_POST['status'])
{
    //passe os dados pra um unico array.
    $dados = array(Coluna => valor
        id_edital => $_POST['status'],
        ....)
}

function insertInto ($tabela, $dados) {

    //use o foreach para ler os dados vindos do array e adicionalos a outro array
    foreach($dados as $key => $value){
                $campos[] = $key; // recebe as colunas
                $valores[] = $value; // recebe os valores das colunas
    }

    //com o implode ele montara a string SQL
    $sql = 'INSERT INTO '.$tabela.'(';
    $sql .= implode(', ' $campos).') VALUES ('. implode(', ', $valores).')';

    $handle = mysql_query($sql, $conexaobd) or die(mysql_error());

    if(!$this->query($sql, $coluna)->error()){
        return true;
    }

    return false;
    }

And an example using PDO :

public static function insert($tabela, $dados){
            $sql = 'INSERT INTO '.$tabela.' (';
            foreach($dados as $key => $value):
                $campos[] = $key;
                $tokens[] = '?';
                $valores[] = $value;
            endforeach;
            try{
                $sql .= implode(', ', $campos).') VALUES ('.implode(', ', $tokens).')';
                $query = self::$conexao->prepare($sql);
                $query->execute($valores);
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }
        }

In $dados you pass Array with the data that will be inserted into the table. Ex:

array(  id_edital =>  valor,
        login_usuario =>  valor
     , ...
     )

You can get your if ($_POST['status_atual'] != $_POST['status']) and play within Array as an example above, turning the columns into Key and the Values into Value . and pass it to the function Insert ;

    
23.03.2015 / 14:10