5. Conexão com o MySQL <?php $link = mysql_connect( 'localhost' , 'root' , '' ); if (! $link ) { die ( 'Não foi possível conectar: ' . mysql_error()); } echo 'Conexão bem sucedida' ; mysql_close( $link ); ?>
6.
7.
8. Consulta e Exibição <? $conexao = mysql_connect( 'localhost' , 'root' , '' ) or die ( "Erro ao efetuar conexão." ); mysql_select_db( "prog_web" ); $result = mysql_query( "SELECT * FROM pessoas" , $conexao ) or die ( " ERRO na consulta " . mysql_error()); ?> < html > < head >< title > CEFET-PHB </ title ></ head > < body > < h1 > Pessoas </ h1 > < table border = "1" > < tr >< th > Nome </ th >< th > Fone </ th >< th > E-Mail </ th ></ tr > <? while ( $pessoa = mysql_fetch_array( $result )) { ?> < tr > < td > <? = $pessoa [ 'nome' ] ?> </ td > < td > <? = $pessoa [ 'fone' ] ?> </ td > < td > <? = $pessoa [ 'email' ] ?> </ td > </ tr > <? } ?> </ table > </ body > </ html >
9. Criação de Base de Dados <?php $con = mysql_connect( 'localhost' , 'root' , '' ); if (! $con ) { die ( 'Não foi possível conectar: ' . mysql_error()); } $sql = 'CREATE DATABASE prog_web' ; if (mysql_query( $sql , $con )) { echo 'A base de dados foi criada' ; } else { echo 'Erro ao criar a BD: ' . mysql_error(); } ?>
10.
11.
12. CRUD - db_conecta.php <?php $conexao = mysql_connect( 'localhost' , 'root' , '' ) or die ( 'Erro ao efetuar conexão.' ); mysql_select_db( 'prog_web' ); ?>
13. CRUD - pessoas_lista.php <?php include_once ( 'db_conecta.php' ); $result = mysql_query( "SELECT * FROM pessoas" , $conexao ) or die ( 'ERRO na consulta: ' . mysql_error()); ?> < html > < head >< title > CEFET-PHB </ title ></ head > < body > < h1 > Pessoas - Lista </ h1 > < table border = "1" > < tr >< th > Nome </ th >< th > Fone </ th > < th > E-Mail </ th >< th colspan = "3" > Ações </ th ></ tr > <? while ( $pessoa = mysql_fetch_array( $result )) { ?> < tr > < td > <? = $pessoa [ 'nome' ] ?> </ td > < td > <? = $pessoa [ 'fone' ] ?> </ td > < td > <? = $pessoa [ 'email' ] ?> </ td > < td >< a href = " pessoas_mostra.php?id= <? = $pessoa [ 'id' ] ?> " > Mostra </ a ></ td > < td >< a href = " pessoas_edita.php?id= <? = $pessoa [ 'id' ] ?> " > Edita </ a ></ td > < td >< a href = " pessoas_exclui.php?id= <? = $pessoa [ 'id' ] ?> " onclick = "return confirm('Tem certeza?');" > Exclui </ a ></ td > </ td > </ tr > <? } ?> </ table > < p >< a href = "pessoas_insere.php" > Insere </ a ></ p > </ body > </ html >
14. CRUD - pessoas_mostra.php <?php include_once ( 'db_conecta.php' ); $id = $_GET [ 'id' ]; $result = mysql_query( "SELECT * FROM pessoas where id=' $id '" , $conexao ) or die ( 'ERRO na consulta: ' . mysql_error()); $pessoa = mysql_fetch_array( $result ); ?> < html > < head >< title > CEFET-PHB </ title ></ head > < body > < h1 > Pessoas - Mostra </ h1 > < p > Nome: <? = $pessoa [ 'nome' ] ?> </ p > < p > Telefone: <? = $pessoa [ 'fone' ] ?> </ p > < p > E-Mail: <? = $pessoa [ 'email' ] ?> </ p > < p >< a href = "pessoas_lista.php" > Volta </ a ></ p > </ body > </ html >
15. CRUD - pessoas_exclui.php <?php include_once ( 'db_conecta.php' ); $id = $_GET [ 'id' ]; mysql_query( "DELETE FROM pessoas where id=' $id '" , $conexao ) or die ( ' ERRO na exclusão: ' . mysql_error()); header( 'Location: pessoas_lista.php' ); ?>
16. CRUD - pessoas_insere.php <?php include_once ( 'db_conecta.php' ); $pessoa [ 'nome' ] = $_POST [ 'nome' ]; $pessoa [ 'fone' ] = $_POST [ 'fone' ]; $pessoa [ 'email' ] = $_POST [ 'email' ]; if (strlen(trim( $_POST [ 'nome' ])) > 0 ) { mysql_query( "insert into pessoas (nome,fone,email) values (' { $pessoa [ 'nome' ]} ',' { $pessoa [ 'fone' ]} ', ' { $pessoa [ 'email' ]} ')" ) or die ( 'Problema ao realizar operação: ' . mysql_error()); header( "Location: pessoas_lista.php" ); } ?> < html > < head >< title > CEFET-PHB </ title ></ head > < body > < h1 > Pessoas - Insere </ h1 > < form action = "pessoas_insere.php" method = "post" > <?php include_once ( 'pessoas_form.php' ); ?> < p >< input type = "submit" value = "Insere" /></ p > </ form > < p >< a href = "pessoas_lista.php" > Volta </ a ></ p > </ body > </ html >
17. CRUD - pessoas_form.php < p > Nome < br />< input type = "text" name = "nome" value = " <? = $pessoa [ 'nome' ] ?> " /></ p > < p > Telefone < br />< input type = "text" name = "fone" value = " <? = $pessoa [ 'fone' ] ?> " /></ p > < p > E-Mail < br />< input type = "text" name = "email" value = " <? = $pessoa [ 'email' ] ?> " /></ p >
18. CRUD - pessoas_edita.php <?php include_once ( 'db_conecta.php' ); $result = mysql_query( "SELECT * FROM pessoas where id=' { $_GET [ 'id' ]} '" , $conexao ) or die ( " ERRO na consulta " . mysql_error()); $pessoa = mysql_fetch_array( $result ); if (strlen(trim( $_POST [ 'nome' ])) > 0 ) { mysql_query( "update pessoas set nome=' { $_POST [ 'nome' ]} ', fone=' { $_POST [ 'fone' ]} ', email=' { $_POST [ 'email' ]} ' where id=' { $_GET [ 'id' ]} '" ) or die ( 'Problema ao realizar operação: ' . mysql_error()); header( "Location: pessoas_lista.php" ); } ?> < html > < head >< title > CEFET-PHB </ title ></ head > < body > < h1 > Pessoas - Edita </ h1 > < form action = " pessoas_edita.php?id= <? = $_GET [ 'id' ] ?> " method = "post" > <?php include_once ( 'pessoas_form.php' ); ?> < p >< input type = "submit" value = "Atualiza" /></ p > </ form > < p >< a href = "pessoas_lista.php" > Volta </ a ></ p > </ body > </ html >