Help to send PHP data to DB

Asked

Viewed 100 times

-2

I’m new to programming and I’d like some help I set up a page to insert dates in 4 fields and need these dates to be stored in DB And I kind of don’t know and I’m having a hard time putting it together...?

Another thing I need to show this date later on the same page...and I don’t know if I did it right.....

Note: I mounted the pag on an Arq ctp and the pag function calls on a controller

function in the 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 appreciate your help/

  • Start well don’t use the mysql_x functions, use mysqli_x or Pdo. Where the Insert?

1 answer

1


Good night,

If it’s starting, start by learning about PDO or mysqli, it’s easier.

In your code , is missing the data Internet , these 4 dates you take from a form where someone informs them?

Let’s assume so. 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 insert :

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 you were successful in writing the data and 0 if you were unable to write the data.

So, you check, if the function returns 1 , it makes a select in the bank 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 is the 3 functions that I believe you will need to use , to select in the bank Voce checks the return of function insert Minutes

I hope I can help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.