Jump to content

conexao não grava no banco


Carlos Rocha
 Share

Recommended Posts

Estou começado em PHP OO e não estou conseguindo gravar no MySQL com MySQLi

Minha estrutura de pastas:

index.php
indexConteudo.php
/controlls
    /daos
           AdminsDao.php
    /models
           Admins.php
    /utils
          Conexao.php

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP OO</title>
</head>
<body>

 <?php 
   require_once ("indexConteudo.php");
 ?>

</body>
</html>

indexConteudo.php

<?php
require_once "controlls/util/Conexao.php";
require_once "controlls/models/Admins.php";
require_once "controlls/daos/AdminsDao.php";


$connection = new Conexao();
$conexao = $connection->abreConexao();
$AdminsDao = new AdminsDao($conexao);

if ($_GET["acao"] == "formulario") {
?>

 <form action="" method="post">
   <input type="hidden" name="acao" value="cadastrar" /> <br />
   <input type="text" name="tipo" /> <br />
   <input type="text" name="nome" /> <br />
   <input type="text" name="login" /> <br />
   <input type="text" name="senha" /> <br /> <br />
   <input type="submit" value="Enviar" />
 </form>

<?php 
}

if ($_POST["acao"] == "cadastrar") {

$tipo = $_POST["tipo"];
$nome = $_POST["nome"];
$login = $_POST["login"];
$senha = $_POST["senha"];

if( $tipo == "" || $nome == "" || $login == "" || $senha == "" ) {
echo "erro";
}
else
{

if( strlen($login) <> 4 || strlen($senha) <> 4 ) {
echo "Login ou Senha com quantidade de caracters errado!";
}
else
{

$admin = new Admins($nome, $tipo, $login, $senha);

$AdminsDao->cadastrar($admin);

echo "Cadastro OK";

$connection->fechaConexao();

}
} 

}


if ($_POST["acao"] == "editar") {

$nome = $_POST["nome"];
$tipo = $_POST["tipo"];
$login = $_POST["login"];
$senha = $_POST["senha"];

if( !($nome != "" || $tipo != "" || $login != "" || $senha != "" ) ) {

if( strlen($login) <> 10 || strlen($senha) <> 10 ) {
echo "Login ou Senha com quantidade de caracters errado!";
}
else
{ 
$admin = new Admins($nome, $tipo, $login, $senha);
$admin->setIdAdmins($_GET["idAdmins"]);

$AdminsDao->editar($admin);

$connection->fechaConexao();

echo "Alteração OK"; 
}
}
else
{
echo "erro";
} 

}

?>

Conexao.php

<?php
class Conexao {

   public function abreConexao() {
       if ($conexao != null)
{
           return $conexao;
}
       else {

$host     = "localhost";
               $db       = "provedor";
               $user     = "carcleo";
               $password = "carcleo";

               $conexao = new mysqli(host,user,pass,db,3306);

               $conexao->set_charset("utf8");    

               return $conexao;
       }

   }

public function fechaConexao () {

       if ($conexao != null) {

           $conexao = null;

}

}
}

Admins.php

<?php 

class Admins {

private $idAdmins;
private $nome;
private $tipo;
private $login;
private $senha;

public function __construct ($_nome, $_tipo, $_login, $_senha) { 
$this->nome = $_nome;
$this->tipo = $_tipo;
$this->login = $_login;
$this->senha = $_senha;
}

public function setIdAdmins ($_idAdmins) {
$this->idAdmins = $_idAdmins;
}

public function getIdAdmins () {
return $this->idAdmins;
}

public function getNome () {
return $this->nome;
}

public function getTipo () {
return $this->tipo;
}

public function getLogin () {
return $this->login;
}

public function getSenha () {
return $this->senha;
}

}
?>

AdminsDao.php

<?php 

class AdminsDao {
private $conexao;

public function __construct ($_conexao) { 
   $this->conexao = $_conexao;
}

public function cadastrar ($admin) { 

   $string = "INSERT INTO admins (tipo, nome, login, senha) VALUES ('".$admin->getTipo()."','".$admin->getNome()."','".$admin->getLogin()."','".$admin->getSenha()."')";

print $string;
echo $this->conexao->host_info . "\n";

$this->conexao->query($string);

}

public function editar ($admin) { 

   $string = "UPDATE admins SET nome = '".$admin->getNome()."', tipo = '".$admin->getTipo()."', login = '".$admin->getLogin()."', senha = '".$admin->getSenha()."' WHERE idAdmins = ".$admin->getIdAdmins();

print $string;
//print $this->conexao;

$this->conexao->query($string);
}

}
?>

Porque será que não grava no banco?

Os dados da conexão estão corretos.

Obs.: A string de conexão imprime corretamente mas o

 $this->conexao->query($string);

Não roda

Edited by carcleo
Link to comment
Share on other sites

Perdão!

Achei o erro:

..........
$conexao = new mysqli($host, $user, $password, $db);
..........

Estava assim:

..........
$conexao = new mysqli(host, user, pass, db);
..........
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.