miguelmkx Posted April 17, 2015 at 04:42 PM Report Share #581424 Posted April 17, 2015 at 04:42 PM (edited) Boa tarde, estou com este código neste momento: <?php require_once "header.php"; require_once "funcoes_sql.php"; require_once "base_dados.php"; require_once "utilitarios.php"; require_once 'mysql.php'; $media =""; $melhor =""; $pior =""; $connect = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD)ordie('Erro ao ligar ao servidor...'); mysql_select_db(MYSQL_DATABASE, $connect)ordie('Erro ao selecionar a base de dados...'); if(isset($_POST['calc'])){ $numeroAluno = $_POST["num'.$i.'"]; $nome = $_POST["nome'.$i.'"]; $curso = $_POST["curso'.$i.'"]; $disciplina = $_POST["disc'.$i.'"]; $notaFinal = $_POST["ava'.$i.'"]; $queryqq ="SET foreign_key_checks = 0;"; $sqlqq = mysql_query($queryqq); $queryForm ="INSERT INTO `carlos`.`dados` (`data_avalicao`, `disciplinas_cod_disciplinas`, `cursos_cod_curso`, `nome`, `avalicao`, `numeroAluno`) VALUES"; $queryForm .="('', '$disciplina','$curso', '$nome','$notaFinal', '$numeroAluno')"; $sqlForm = mysql_query($queryForm)ordie("Erro query!"); } ?> <!DOCTYPE html> <html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"type="text/css"href="css.css"> </head> <body> <h2>Avaliação dos Alunos</h2> <formaction="avaliacoes.php"method="get"> Alunos a avaliar: <inputtype="text"name="nval"/> <inputtype="submit"Value="Seguinte"name="seguinte"/> </form> <?php if(isset($_GET['seguinte'])){ $n=$_GET['nval']; echo '<form action="avaliacoes.php" method="post">'; echo '<table border="1"> <tr> <th>Número Aluno</th> <th>Nome Aluno</th> <th>Curso</th> <th>Disciplina</th> <th>Avaliação</th> </tr>'; for($i=1; $i<=$n; $i++){ echo '<tr> <td><input type="text" name="num'.$i.'"></td> <td><input type="text" name="nome'.$i.'"></td> <td><input type="text" name="curso'.$i.'"></td> <td><input type="text" name="disc'.$i.'"></td> <td><input type="text" name="ava'.$i.'"></td></tr>'; } echo '</table>'; echo '<input type="hidden" name="nval" value="'.$n.'" />'; echo '<input type="submit" Value="Calcular" name="calc"> </form>'; } if(isset($_GET['calc'])){ $n=$_GET['nval']; $soma=0; echo '<table border="1" class="tabela"> <tr> <th>Número Aluno</th> <th>Nome Aluno</th> <th>Curso</th> <th>Disciplina</th> <th>Avaliação</th> </tr>'; for($i=1; $i<=$n; $i++){ $soma=$soma+$_GET['ava'.$i]; $melhor = $_GET['ava'.$i]; $pior = $_GET['ava'.$i]; $array = max(str_split($melhor)); $array2 = min(str_split($pior)); if($i%2==0){ $bg_cor='gray';}else{$bg_cor='white';} echo '<tr style="background-color:'.$bg_cor.'"> <td>'.$_GET['num'.$i].'</td> <td>'.$_GET['nome'.$i].'</td> <td>'.$_GET['curso'.$i].'</td> <td>'.$_GET['disc'.$i].'</td> <td>'.$_GET['ava'.$i].'</td></tr>'; } echo '</table>'; $media=$soma/$n; } echo "<h3>Média Notas: $media</h3>"; echo "<h3>Melhor Nota: $melhor </h3>"; echo "<h3>Pior Nota: $pior</h3>"; ?> </body> </html> A minha dúvida é inserir os valores do formulário na Base de Dados. Uma vez que por exemplo: $numeroAluno = $_POST["num'.$i.'"]; , o php não reconhece a variavél $i. Ajuda? Obrigado. Edited April 17, 2015 at 05:43 PM by apocsantos geshi Link to comment Share on other sites More sharing options...
bioshock Posted April 17, 2015 at 06:42 PM Report Share #581430 Posted April 17, 2015 at 06:42 PM $length = $_POST['nval']; for($i = 0; $i <= $length; i++) { $numeroAluno = $_POST['num' . $i]; $nome = $_POST['nome' . $i]; // etc.. if($numeroAluno == null || $nome == null) continue; // salta fora, significa que os campos não foram preenchidos $query = ....etc } Link to comment Share on other sites More sharing options...
chbcl Posted April 17, 2015 at 07:19 PM Report Share #581433 Posted April 17, 2015 at 07:19 PM estranho que o PHP não te esteja a dar erro no 'ordie' nas primeiras linhas ... developer @ filipealves.net filipealvesbcl [a] gmail.com github.com/filipealvesbcl Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now