FernandoPaiva
FernandoPaiva

Reputation: 4462

Only variables should be passed by reference?

I'm creating a simple PHP script to insert some values in my data base. To do it I created a Data Access Object to send the object to insert. The values of object are inserted in my data base, but throws this exception: Only variables should be passed by reference.

How could I solve it ?

Object

class Local{
    private $id;
    private $nome;
    private $telefone;  
    private $endereco;
    private $numero;
    private $bairro;    
    private $cidade;
    private $tipoLocal;
    private $status;
    private $image;
//gets and sets

Insert

/** insert the object */
    public function insert(Local $local){       
        $stm = $this->conexao->prepare("INSERT INTO locais (nome, telefone, 
                                    endereco, numero, bairro,
                                    cidade_id, tipo_local_id, 
                                    status, image) 
                                        VALUES (?,?,?,?,?,?,?,?,?)");
        $stm->bindParam(1,  $local->getNome());
        $stm->bindParam(2,  $local->getTelefone());     
        $stm->bindParam(3,  $local->getEndereco());
        $stm->bindParam(4,  $local->getNumero());
        $stm->bindParam(5,  $local->getBairro());       
        $stm->bindParam(6,  $local->getCidade()->getId());
        $stm->bindParam(7,  $local->getTipoLocal()->getId());
        $stm->bindParam(8, $local->getStatus());
        $stm->bindParam(9, $local->getImage());

        $stm->execute();
        if($stm){
            return 1;
        }else{
            return 0;
        }
    }

Action to insert

if (!empty($_POST)){    

    //post
    $nome = $_POST["nome"];
    $telefone = $_POST["telefone"];
    $endereco = $_POST["endereco"];
    $numero = $_POST["numero"];
    $bairro = $_POST["bairro"];
    $cidadeId = $_POST["cidade_id"];
    $tipoLocalId = $_POST["tipo_local_id"];

    //instancia de objetos
    $local = new Local();
    $cidade = new Cidade();
    $tipoLocal = new TipoLocal();

    $local->setNome($nome); 
    $local->setTelefone($telefone);            
    $local->setEndereco($endereco);
    $local->setNumero($numero);
    $local->setBairro($bairro);            
    $local->setStatus(1);
    $local->setImage($telefone.".jpg");
    //cidade
    $cidade->setId($cidadeId);
    $local->setCidade($cidade);
    //tipo local
    $tipoLocal->setId($tipoLocalId);
    $local->setTipoLocal($tipoLocal);


    //imagem do usuario
    $arquivo = isset($_FILES["file"]) ? $_FILES["file"] : FALSE;
    $type = $arquivo["type"];

    $arr;
    if(!$arquivo) {
        $arr = array("status" => 0, "msg" => "Você não pode acessar essa imagem diretamente");
    }elseif ($type != "image/jpg" && $type != "image/png" && $type != "image/jpeg") {
        $arr = array("status" => 0, "msg" => "Não é um tipo de imagem válida");
    }else{
        if(isLocalExist($local) == 0){
            $insere = insert($local);
            if($insere == 1){
                $diretorio = "../imagens/";
                copy($arquivo["tmp_name"], $diretorio.$telefone.".jpg");
                $arr = array("status" => $insere, "msg" => "Inserido com sucesso");                
            }else{
                $arr = array("status" => $insere, "msg" => "Não foi possível inserir");
            }            
        }else{
            $arr = array("status" => 0, "msg" => "Local já cadastrado!");            
        }       

    }
    echo json_encode($arr);



}else{
    $arr = array("status" => 0, "msg" => "Post is empty!");
    echo json_encode($arr);
}

/** verifica se o local ja esta cadastrado */
function isLocalExist(Local $local){
    $dao = new LocalDAO();
    $existe = $dao->isLocalExiste($local->getNome(),                                   
                                  $local->getTipoLocal()->getId(), 
                                  $local->getCidade()->getId(),
                                  $local->getEndereco(), 
                                  $local->getTelefone());
    return $existe;    
}


/** insert object */
function insert(Local $local){
    $dao = new LocalDAO();
    $inserido = $dao->insert($local);
    return $inserido;
}

Exception

<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>22</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>23</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>24</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>25</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>26</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>27</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>28</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>29</b>
<br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in 
<b>/opt/lampp/htdocs/TelefonesUteis/dao/LocalDAO.php</b> on line 
<b>30</b>
<br />

Upvotes: 0

Views: 362

Answers (1)

EddyTheDove
EddyTheDove

Reputation: 13259

Not sure, but try this

 public function insert(Local $local){       
            $stm = $this->conexao->prepare("INSERT INTO locais SET nome = :nome, telefone = :telefone, endereco = :endereco, numero = :numero, bairro = :bairro, cidade_id = :cidade_id, tipo_local_id = :tipo_local_id, status = :status, image = :image");

            $stm->bindValue(':nome',  $local->getNome());
            $stm->bindValue(':telefone',  $local->getTelefone()); 
            ...

            $stm->execute();
            if($stm){
                return 1;
            }else{
                return 0;
            }
        }

Let me know if this helped

Upvotes: 1

Related Questions