Help to send data from PHP to DB

-2

I'm new to the programming area and would like some help I set up a page to insert dates into 4 fields and I need those dates to be stored in DB And I kind of do not know and I'm having trouble riding ... can anyone help me?

Another thing I need to show this date later on the same page ... and I do not know if I did it right ... is commented not to give an error on the page ..

Note: I set the pag in a ctp file and the pag function calls in a controller

function in controller:

   public function desligamentocliente()
{
    $this->set( "titulo_da_pagina", "Desligamento de Cliente");
    $idCliente = ( isset( $this->request->params['pass'][0] ) ) ? $this->request->params['pass'][0] : 0;


       //$this->verifica_ausencia_dados( $this->request->data['idCliente'] );
        $this->loadModel( "DesligamentoCliente" );

                   $connect = mysql_connect('nome_do_servidor', 'nome_de_usuario', 'senha');
                    $db = mysql_select_db('nome_bo_banco_de_dados');
                    $query_select = "SELECT login FROM usuarios WHERE login = '$login'";

       $dados = $this->DesligamentoCliente->find( "list", array( "conditions" => array(
            "_esc_codigo" => $idCliente )));
        /*if ($dados['Desligamentos']['id'] ='' ) {
           echo "Data": $dados;
        } else {
            if ($dados['Desligamentos']['id'] == "" || $dados['Desligamentos']['id'] = null) {
            echo "Data não cadastrada";
            }
        }*/

}

I thank you for the help

    
asked by anonymous 06.11.2015 / 20:20

1 answer

1

Good evening,

Since it's beginning, start learning about PDO mysqli, it's easier.

In your code, you are missing the data insert, these 4 dates you get from a form where someone informs them?

Let's say yes. , then you would do the insert:

function conexao(){

    $dbuser = "root";
    $dbpass = "1234";

    try {

        $pdo = new PDO('mysql:host=localhost;dbname=crm',  $dbuser, $dbpass);
        $pdo -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
        $pdo->exec("SET CHARACTER SET utf8");//corrige os acentos na hora de gravar no BD
    } catch(Exception $e) {

        echo 'Erro na conexao: ' . $e->getMessage();
    }
}

Function to enter:

function inserirDatas($pdo){
    try {
                $data_1 = $_POST['data_1'];
                $data_2 = $_POST['data_2'];
                $arr = array();
                $sql = 'INSERT INTO suaTabela(campo1,campo2)VALUES(:data_1,:data_2)';
                $stmt = $pdo->prepare($sql);
                $dados = array(
                    ':data_1' => $data_1,
                    ':data_2' => $data_2
                );

                $stmt-> execute($dados);
                $linha = $stmt->rowCount();
                    if($linha == 1){

                        $arr['id'] = $stmt = $pdo->lastInsertId();
                        $arr['retorno'] = 1;
                    }else{
                        $arr['retorno'] = 0;
                    }

                $conexao = desconecta($conexao);
            } catch(Exception $e) {
                $resultado = 'Erro ao inserir os dados no banco: ' . $e->getMessage();
                $conexao = desconecta($conexao);
            }

return $arr;

}

Here in the function, it returns 1 if it succeeded in writing the data and 0 if it could not write the data.

So, you check, if the function returns 1, it does a select on the bank by bringing the dates

function pegaUltimaData($pdo,$id){

        try{ 

            $sql = "SELECT * from suaTabela where id = $id";
            $stmt = $pdo->prepare($sql);
            $stmt->execute();
            if($stmt->rowCount() >= 1){
                $linha = $stmt->fetchAll(PDO::FETCH_ASSOC);
                return $linha;              
            }

        } catch (Exception $e){ 

            print "Ocorreu um erro ao tentar executar esta ação";
            echo  "Erro: Código: " . $e-> getCode() . " Mensagem: " . $e->getMessage();
        }

    }

Above you have the 3 functions that I believe you will have to use, to make the select in the database you check the return of the insertDatas

I hope you can help.

    
06.11.2015 / 22:06