Skip to content

Instantly share code, notes, and snippets.

@crystianwendel
Created November 8, 2013 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crystianwendel/7371491 to your computer and use it in GitHub Desktop.
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
<?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>
<?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>
<?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