insertion in related tables

1
Hello, I'm working with some related tables and I can not insert the values in the next rabla, I discovered that I need to retrieve the last id inserted in the I still can not use it, without the relation the tables work perfectly. Follow the commands. NOTE: I do not execute the same one does not return any error.

Client Class

public function sendDado(){

  if($_POST){
    try{
      $query = $this->db->prepare("INSERT INTO CLIENTE(nome, email) values(:nome,:email)");
      $query->bindValue(":nome", $_POST['nome'], PDO::PARAM_STR);
      $query->bindValue(":email", $_POST['email'], PDO::PARAM_STR);
      $query->execute();
      echo "Enviado com sucesso";
      header('Location: telefone.tpl.php');
      //return ($query);
    }catch(PDOException $e){
        echo "Não foi possivel enviar";
    }
  }else{
      echo "";
  }

    return $query;
}

Phone Class

public function sendDado($scan){

    //$scan = new Cliente();
    //$esse->scan = $scan->sendDado();
    //$esse-> sendDado($scan);

    $u_id = $this->db->query("SELECT LAST_INSERT_ID()");

    if($u_id != null){
        if($_POST){
            try{
                $query = $this->db->prepare("INSERT INTO TELEFONES(rel_id,telefone,celular,ramal) values (:u_id,:telefone,:celular,:ramal)");
                $query->bindValue(":u_id",$_POST['u_id'],PDO::PARAM_INT);
                $query->bindValue(":telefone",$_POST['telefone'],PDO::PARAM_INT);
                $query->bindValue("celular",$_POST['celular'],PDO::PARAM_INT);
                $query->bindValue("ramal",$_POST['ramal'],PDO::PARAM_INT);
                $query->execute();
                echo "Enviado com sucesso";

                //if(){
                    header('Location: emp.tpl.php');
                //)else{
                    //header('Location: ');
                //}
            }catch(PDOException $e){
                echo 'Informacao não pode ser enviada';
            }

        }

    }else{
        echo $u_id;
        //header('Location: cliente.tpl.php');
    }
}
    
asked by anonymous 28.12.2016 / 21:34

1 answer

0

Since your code is not complete, I'm not exactly sure how your implementation was.

NOTE: I would not use header('Location:.... , would make the CLIENT class instantiating the class PHONE.

Anyway, here's a solution based on your code:

Client Class

error_reporting (E_ALL);

public function sendDado(){

  if($_POST){
    try{
      $query = $this->db->prepare("INSERT INTO CLIENTE(nome, email) values(:nome,:email)");
      $query->bindValue(":nome", $_POST['nome'], PDO::PARAM_STR);
      $query->bindValue(":email", $_POST['email'], PDO::PARAM_STR);
      $query->execute();

      // lê o ID do cliente inserido
      $cliente_id = $this->db->lastInsertId();  

      echo "Enviado com sucesso";

      // passa ID do cliente via GET para TELEFONE
      header('Location: telefone.tpl.php?id_cliente=' . $cliente_id); 
      //return ($query);
    }catch(PDOException $e){
        echo "Não foi possivel enviar";
    }
  }else{
      echo "";
  }

    return $query;
}

Phone Class

error_reporting (E_ALL);

public function sendDado($scan) {

// RECEBE o GET com o ID DO CLIENTE e filtra, deixando só inteiros
$u_id = filter_input(INPUT_GET, 'id_cliente', FILTER_VALIDATE_INT, 1);

if ($u_id != null) {

    try {
        $query = $this->db->prepare("INSERT INTO TELEFONES(rel_id,telefone,celular,ramal) values (:u_id,:telefone,:celular,:ramal)");
        $query->bindValue(":u_id", $_POST['u_id'], PDO::PARAM_INT);
        $query->bindValue(":telefone", $_POST['telefone'], PDO::PARAM_INT);
        $query->bindValue("celular", $_POST['celular'], PDO::PARAM_INT);
        $query->bindValue("ramal", $_POST['ramal'], PDO::PARAM_INT);
        $query->execute();
        echo "Enviado com sucesso";

        //if(){
        header('Location: emp.tpl.php');
        //)else{
        //header('Location: ');
        //}
    } catch (PDOException $e) {
        echo 'Informacao não pode ser enviada';
    }
} else {
    echo $u_id;
    //header('Location: cliente.tpl.php');
}
}

I hope I have helped.

    
28.12.2016 / 22:18