Created
July 19, 2016 23:28
-
-
Save joffilyfe/7815efe03c5ed5e707240f82e23e9e4b to your computer and use it in GitHub Desktop.
Inserção de alunos com prepare statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Exercício 3 - Slides PDO | |
* Este exercício realiza a operação de cadastro de alunos utilizando PREPARE e BINDPARAM: | |
* @nome, @curso_id | |
* Os cursos precisam ser mostrados por meio de um select | |
*/ | |
$consulta = "SELECT * FROM curso"; | |
$sql_alunos = "SELECT * FROM aluno"; | |
try { | |
$user = 'root'; | |
$password = ''; | |
$conexao = new PDO('mysql:host=localhost;dbname=estudar_php', $user, $password); | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
die(); | |
} | |
/* | |
* Checamos se foi feito um POST | |
* ~> checamos se o nome do aluno foi informado (o nome precisa ter ao menos 1 caracter) | |
* ~> checamos se o curso foi informado | |
* Executamos um INSERT na tabela aluno com os parâmetros passado via formulário | |
* Se o resultado for 1 (resultado do exec) mostramos a mensagem de sucesso. | |
*/ | |
if ($_POST) { | |
if (!isset($_POST['nome_aluno']) || strlen($_POST['nome_aluno']) == 0) { | |
echo 'Por favor informe o nome do aluno'; | |
} elseif (!isset($_POST['curso'])) { | |
echo 'Por favor informe um curso'; | |
} else { | |
/* | |
* Utilizamos o prepare statement para criar um SQL seguro | |
* Os valores são passados posteriormente utilizando o bindParam | |
*/ | |
$insert = $conexao->prepare("INSERT INTO aluno(nome, curso_id) VALUES (:param_1, :param_2)"); | |
$insert->bindParam(":param_1", $_POST['nome_aluno'], PDO::PARAM_STR); | |
$insert->bindParam(":param_2", $_POST['curso'], PDO::PARAM_INT); | |
if ($insert->execute()) { | |
echo "O aluno foi cadastrado com sucesso.."; | |
} | |
} | |
} | |
?> | |
<html> | |
<head> | |
<style> | |
form { | |
width: 40%; | |
display: inline-block; | |
} | |
.list { | |
display: 50%; | |
display: inline-block; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<!-- Formulário --> | |
<form method="POST"> | |
<fieldset> | |
<input type="text" name="nome_aluno" placeholder="Nome do aluno"> | |
</fieldset> | |
<fieldset> | |
<label for="curso">Curso</label> | |
<select name="curso" id="curso"> | |
<option>Selecione um curso</option> | |
<?php foreach($conexao->query($consulta) as $curso): ?> | |
<option value="<?php echo $curso['cursoid'];?>" > | |
<?php echo $curso['nomecurso']; ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
</fieldset> | |
<fieldset> | |
<button>Matricular</button> | |
</fieldset> | |
</form> | |
<!-- Listagem de alunos --> | |
<ul class="list"> | |
<?php foreach($conexao->query($sql_alunos) as $aluno): ?> | |
<li> | |
<?php echo $aluno['nome'], ' curso: ', $aluno['curso_id']; ?> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment