Insert and PDO problem

Asked

Viewed 221 times

1

I am trying to make an Internet with PDO. For this I use:

controller/con_clients.add.php

<?php
if($action == "new_client"){
    $search_cod_cliente = $pdo->prepare("SELECT MAX(cli_cod_cliente) AS cli_cod_cliente FROM clients"); 
    $search_cod_cliente->execute();

    while($result_cod_cliente = $search_cod_cliente->fetchObject()){
        $cli_cod_cliente = $result_cod_cliente->cli_cod_cliente + 1;
    }

    $conectar = new Con_Clients_Add;
    $conectar = $conectar->con_clients_add($cli_cod_cliente);
}
?>

classes/Con_clients_add.class.php

<?php
    class Con_Clients_Add{
        public function con_clients_add($cli_cod_cliente){
            try {
                $pdo = new PDO('mysql:host='.$host.';dbname='.$database, $login_db, $senha_db);
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch(PDOException $e) {
                echo 'ERROR: ' . $e->getMessage();
            }

            $client_insert = $pdo->prepare("INSERT INTO clients(cli_cod_cliente) VALUES (:cli_cod_cliente)");
            $client_insert->bindParam(':cli_cod_cliente', $cli_cod_cliente, PDO::PARAM_STR);
            $client_insert->execute(); 
        }
    }
?>

But if I use the traditional method, it works:

$db = mysql_connect ($host, $login_db, $senha_db);
$basedados = mysql_select_db($database);

$insere_cliente = mysql_query("INSERT INTO clients(cli_cod_cliente) VALUES ('$cli_cod_cliente')");

Thank you for the force. Hugs. Rafael

  • 1

    Any mistakes? What happens?

  • Yeah... Error appears 500

  • This echo 'ERROR: ' . $e->getMessage(); doesn’t happen?

  • No... The funny thing is that the $cli_cod_client variable comes from the/con_clients_add.php... But if I force a value, for example: $cli_cod_client = '1900'; there it works...

  • I’m doubting it has anything to do with this... $client_insert->bindParam(':cli_cod_client', $cli_cod_client, PDO:PARAM_STR);

  • I just think it’s weird because it doesn’t seem to have anything to do with the connection, otherwise it would print 'ERROR: ' . $e->getMessage();

  • Puts this at the beginning of your script, should show errors: ini_set('display_errors', true);&#xA;error_reporting(E_ALL);

Show 2 more comments

1 answer

1

The problem seems to be that your method con_clients_add is being interpreted as class controller, so on the first call.

A method with the same class name in some circumstances is considered as constructor.

The ideal is to define the constructor as __construct() or rename your method.

$conectar = new Con_Clients_Add; //primeira chamada
$conectar = $conectar->con_clients_add($cli_cod_cliente); segunda chamada

Simple example

class abc{
    public function abc(){
        echo 'sou o construtor<br>';
    }
}

$a = new abc();
$a->abc();

Exit:

sou o construtor
sou o construtor
  • 1

    Solved... I changed the name of the method and it worked. Thank you. ?

  • @Miguel the connection can be made successfully, ok, but the query can fail because the id is empty. You can give the Warning Missing argument 1 for

  • But does Insert happen? I had the feeling that it did not. @Rafaelschaffergimenes should accept the answer, below the arrows above/below the left of the answer, welcome

  • @Miguel I think not, as there is no try for him you do not even know if it worked or not, at most a "look has an exception not captured there"

  • My stupidity, the connection was being made clear the Insert is not, correct?

  • @Miguel this, not first nor works only generates warnings, second yes pq $cli_cod_cliente has been defined and has value.

Show 1 more comment

Browser other questions tagged

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