Created
November 8, 2013 14:05
-
-
Save crystianwendel/7371491 to your computer and use it in GitHub Desktop.
Exemplo simples de conexão com o banco, e inserção e lista de uma entidade simples
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 | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<form method="post" action="/InserirAluno.php"> | |
<p>Nome: <input type="text" name="name" placeholder="Nome do aluno"/></p> | |
<p>Profissão: <input type="text" name="profissao" placeholder="Profissão do aluno"/></p> | |
<p><input type="submit" value="Criar" /></p> | |
</form> | |
</body> | |
</html> |
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 | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<?php | |
$con=mysqli_connect("localhost","crystian","12345678","aulasql"); | |
if (mysqli_connect_errno($con)) { | |
echo "Failed to connect to MySQL: " . mysqli_connect_error(); | |
} | |
$result = mysqli_query($con,"SELECT * FROM aLUNOS"); | |
echo count($result) . " Elements."; | |
while($row = mysqli_fetch_array($result)) { | |
echo "<br/>"; | |
echo $row['name'] . " " . $row['profissao']; | |
} | |
?> | |
</body> | |
</html> |
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 | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<?php | |
$con = mysqli_connect("localhost", "crystian", "12345678", "aulasql"); | |
if (mysqli_connect_errno($con)) { | |
die("Erro ao conectar no BD: " . mysqli_error($con)); | |
} else { | |
$sql="INSERT INTO alunos (name, profissao) | |
VALUES | |
('".$_POST["name"]."','".$_POST["profissao"]."')"; | |
echo $sql; | |
if (!mysqli_query($con,$sql)) | |
{ | |
die('Error: ' . mysqli_error($con)); | |
} else { | |
echo "<p>Aluno inserido com sucesso!</p>"; | |
} | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment