Advertisement
luistavares

Usando Select

Apr 1st, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2.     //Recebe dados do formulário
  3.     $id = $_GET['id'];
  4.     $nome = $_GET['nome'];
  5.     $email = $_GET['email'];
  6.     $nota = $_GET['nota'];
  7.     $idCurso = $_GET['idCurso'];
  8.    
  9. ?>
  10.  
  11. <html lang="pt-br">
  12.     <head>
  13.         <meta charset='UTF-8'/>
  14.         <title> Escola </title>
  15.     </head>
  16.     <body>
  17.         <h3> Edição de Aluno </h3>
  18.         <form method='post' action='atualizar.php'>
  19.             <?php
  20.                 print "
  21.                     <input type='hidden' name='id' value='$id'>
  22.                     ========<input type='hidden' name='idCurso' value='$idCurso'>
  23.                     <label>Nome</label>
  24.                     <input name='nome' value='$nome'/><br>
  25.                     <label>E-mail</label>
  26.                     <input name='email' type='email' maxlength='20' value='$email'/><br>
  27.                     <label>Nota</label>
  28.                     <input name='nota' value='$nota'/><br>
  29.                    
  30.                     <label>Curso</label>
  31.                     <select name='cursos'> 
  32.                     <option value='000'>Selecione:</option>
  33.                 ";
  34.                     //Conectando com o BD
  35.                     $ds = 'mysql:host=localhost;dbname=bdif';
  36.                     $user = 'root';
  37.                     $pass = 'vertrigo';
  38.                     $db = new PDO($ds, $user, $pass);
  39.    
  40.                     #Definindo o SQL
  41.                     $query = 'SELECT idCurso, curso FROM tbcurso  order by curso ASC';
  42.                     $stm = $db->prepare($query);
  43.                     if($stm->execute()){
  44.                         while($row = $stm->fetch()){
  45.                             $id = $row['idCurso'];
  46.                             $curso = $row['curso'];
  47.                             if ($idCurso == $id) {
  48.                                   print "<option value='$id'  selected='selected'>$curso</option>";
  49.                             }
  50.                             else {
  51.                                   print "<option value='$id'>$curso</option>";
  52.                             }
  53.                         }  
  54.                     }
  55.                     else{
  56.                         print "<p>Erro ao listar os cursos</p>";
  57.                         print_r($stm->errorInfo());
  58.                     }
  59.                                
  60.                     print "</select>"; 
  61.                    
  62.        
  63.                    
  64.                    
  65.             ?>
  66.             <button type='submit'>atualizar</button>
  67.         </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement