Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Manipula_BD
- {
- private $host="localhost", $user="root", $pass="", $con="", $seldb="", $tabela="";
- private $sql_ins="", $sql_sel="", $sql_upd="", $sql_del="";
- private $ins="", $sel="", $upd, $del="";
- public $menu = "<center><b>Aplicativo Iniciante em PHPOO - Menu Principal</b><br><br></center>", $form="";
- public function __construct($banco, $tabela)
- {
- $this->tabela = $tabela;
- if($this->con = mysql_connect($this->host, $this->user, $this->pass))
- {
- if(!$this->seldb = mysql_select_db($banco, $this->con))
- {
- die ('<center>Erro ao selecionar o banco '. "<br>" .mysql_error())."</center>";
- }
- }
- }
- public function inserir($campos, $valores)
- {
- $this->sql_ins = "INSERT INTO " . $this->tabela . " ($campos) VALUES ($valores)";
- if(!$this->ins = mysql_query($this->sql_ins, $this->con))
- {
- die ("<center>Erro na inclusão <br>" . mysql_error() . "<br>
- <a href='menu.php'>Menu</a></center>");
- }else{
- print "<center><a href='menu.php'>Menu</a></center>";
- }
- }
- public function consultar($campos, $where = NULL, $orderby = NULL)
- {
- if ($where)
- {
- $this->sql_sel = "SELECT $campos FROM " . $this->tabela . " WHERE $where ORDER BY $orderby";
- }else{
- $this->sql_sel = "SELECT $campos FROM " . $this->tabela." ORDER BY $orderby";
- }
- if(!$this->sel = mysql_query($this->sql_sel, $this->con))
- {
- die ("Erro na Consulta <br>" .mysql_error() ."<br><a href='menu.php'>Menu</a>");
- }else{
- $regs = "<center><table border=\"0\"> ";
- $regs .= "<tr><td colspan=\"2\"><h2>Resultado da Consulta</h2></td></tr>";
- $row=0;
- $nf=mysql_num_fields($this->sel);
- $regs .= "<tr>";
- for($x=0;$x < $nf ;$x++){
- $regs .="<td><b>".ucfirst(mysql_field_name($this->sel,$x))."</b></td>";
- }
- $regs .= "</tr>";
- while ( $data = mysql_fetch_array($this->sel)) {
- $regs .= "<tr>";
- for($x=0;$x < $nf;$x++){
- $regs .= "<td>" . $data[$x] . "</td>";
- }
- $row++;
- $regs .= "</tr>";
- }
- if (!$this->sel){
- die ("<center>Erro na Consulta <br>" .mysql_error() ."<br>
- <a href='menu.php'>Menu</a></center>");
- }else{
- $regs .= "</table>";
- print $regs;
- print "<br><br><a href='menu.php'>Voltar ao Menu</a>";
- }
- }
- }
- public function atualizar($camposvalores, $where = NULL)
- {
- if ($where)
- {
- $this->sql_upd = "UPDATE " . $this->tabela . " $camposvalores WHERE $where";
- }else{
- $this->sql_upd = "UPDATE " . $this->tabela . " $camposvalores";
- }
- if(!$this->upd = mysql_query($this->sql_upd, $this->con))
- {
- die ("<center>Erro na atualização <br>" .mysql_error() . "<br><a href='menu.php'>Menu</a></center>");
- }else{
- print "<a href='menu.php'>Voltar ao Menu</a>";
- }
- }
- public function excluir($where = NULL)
- {
- if ($where)
- {
- $this->sql_sel = "SELECT * FROM " . $this->tabela . " WHERE $where";
- $this->sql_del = "DELETE FROM " . $this->tabela . " WHERE $where";
- }else{
- $this->sql_sel = "SELECT * FROM " . $this->tabela;
- $this->sql_del = "DELETE FROM " . $this->tabela;
- }
- $sel=mysql_query($this->sql_sel);
- $regs=mysql_num_rows($sel);
- if ($regs > 0){
- if(!$this->del = mysql_query($this->sql_del, $this->con))
- {
- die ("<center>Erro na exclusão <br>" .mysql_error() ."<br>
- <a href='menu.php'>Menu </a></center>" );
- }else{
- print "<center>Registro Excluído com Sucesso!<br><a href='menu.php'>Menu</a></center>";
- }
- }else{
- print "<center>Registro Não encontrado!<br><a href='menu.php'>Menu</a></center>";
- }
- }
- public function menu ($scripts=array())
- {
- reset($scripts);
- $this->menu .="<center><table border=\"0\"> ";
- while (list($chave, $arquivo) = each ($scripts)) {
- $this->menu .= "<tr><td><a href='$arquivo'>$arquivo</a></td></tr>";
- }
- $this->menu .="</table></center>";
- print $this->menu;
- }
- public function form ($campos=array(), $acao, $nome, $metodo)
- {
- reset($campos);
- $this->form .= "<center><table border=\"0\"> ";
- $this->form .= "<form name=\"$nome\" action=\"$acao\" method=\"$metodo\"><br />\n";
- while (list($chave, $campo) = each ($campos)) {
- $this->form .= "<tr><td>$campo</td><td><input type=\"text\" name=\"$campo\" value=\"\"></td></tr>";
- }
- $this->form .= "<tr><td> </td><td colspan=\"2\"><input type=\"submit\" value=\"Enviar\">";
- $this->form .= " <input type=\"reset\" value=\"Limpar\"></td></tr>";
- $this->form .= "</form>";
- $this->form .= "</table></center>";
- print $this->form;
- }
- public function form_dados($where = NULL, $acao, $metodo, $nome)
- {
- if ($where)
- {
- $this->sql_sel = "SELECT * FROM " . $this->tabela . " WHERE $where";
- }else{
- $this->sql_sel = "SELECT * FROM " . $this->tabela;
- }
- if(!$this->sel = mysql_query($this->sql_sel, $this->con))
- {
- die ("<center>Erro na Consulta <br>" .mysql_error() ."<br>
- <a href='javascript:history.back()'>Voltar</a></center>");
- }else{
- $form = "<form name=$nome method=$metodo action=$acao><center><table border=\"0\">";
- while ($campo = mysql_fetch_array($this->sel, MYSQL_NUM)) {
- $form .= "<tr><td>".mysql_field_name($this->sel,0)."</td><td><input type=\"text\"
- name=".mysql_field_name($this->sel,0)." value=\"$campo[0]\"></td></tr>";
- $form .= "<tr><td>".mysql_field_name($this->sel,1)."</td><td><input type=\"text\"
- name=".mysql_field_name($this->sel,1)." value=\"$campo[1]\"></td></tr>";
- }
- $form .= "<tr><td> </td><td><input type=\"submit\" value=\"Enviar\"> ";
- $form .= " <input type=\"reset\" value=\"Limpar\"></td></tr>";
- $form .= "</table></form>";
- print $form;
- }
- }
- }
- // Deixar o objeto já criado para ter sempre a conexão aberta
- $objCliente = new Manipula_Bd('iniciante', 'cliente'); // Conectar com $banco e $tabela
- ?>
Add Comment
Please, Sign In to add comment