insertion in related tables

Asked

Viewed 81 times

1

Hello, I am working with some related tables and I can not insert the values in the next rabela, I found that I need to recover the last id inserted in the still can not use it, without the relation the tables work perfectly. Follow commands. OBS: Do not execute it does not return any error.

Customer 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;
}

Telephone 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');
    }
}
  • 1

    Wouldn’t it be better to take php? $last = db->lastInsertId(); after running

1 answer

0

Since your code is not complete, I don’t know exactly how it was implemented.

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

Anyway, it follows a solution based on your code:

Customer 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;
}

Telephone 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’ve helped.

  • it still doesn’t work, it looks like it ran normally, but when I go to the table there is no data inserted.

  • On which server are you testing? You can describe your environment?

  • phpmyadmin, I am trying to relate 4 tables client-phone-company-address, the tables have a field defined as foreign key.

  • Hello Cecilia, you can put the following command in your PHP file and check which error is returning: error_reporting (E_ALL);

  • it does not return error, it is as if td right

  • What returns with: var_dump($_POST);

  • in the address bar I see that it is sending the id value, but when using var_dump() to show the value it retrieves "null".

  • Let me know when I’m online.

Show 3 more comments

Browser other questions tagged

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