Skip to content

Instantly share code, notes, and snippets.

@jamezrin
Last active February 1, 2018 15:55
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 jamezrin/33b8862ff5b150e991aaa52bc254adc4 to your computer and use it in GitHub Desktop.
Save jamezrin/33b8862ff5b150e991aaa52bc254adc4 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Pruebas</title>
</head>
<body>
<?php
// Direccion del servidor, usuario, contraseña y base de datos
$con = new mysqli("localhost", "root", "", "cosasdb");
if ($con->connect_error) {
echo "Ha habido un error conectando a la base de datos";
}
$con->query("
CREATE TABLE IF NOT EXISTS cosas (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
creador VARCHAR(30),
texto VARCHAR(255)
)");
if (!empty($_POST)) {
$creador = mysql_real_escape_string($_POST["creador"]);
$cosa = mysql_real_escape_string($_POST["cosa"]);
$con->query("INSERT INTO cosas VALUES (NULL, '$creador', '$cosa')");
}
?>
<p>Lista de cosas: </p>
<?php
$cosas = $con->query("SELECT id, creador, texto FROM cosas");
if ($cosas and $cosas->num_rows > 0) {
echo "<ul>";
while ($cosa = $cosas->fetch_assoc()) {
echo "<li>" . $cosa["creador"] . " ha dicho: " . $cosa["texto"] . "</li>";
}
echo "</ul>";
} else {
echo "No hay nada! Agrega una cosa";
}
?>
<form method="POST" action="index.php">
<label for="creador">Nombre: </label>
<input type="text" name="creador">
<br>
<label for="cosa">Mensaje: </label>
<input type="text" name="cosa">
<br>
<input type="submit" value="Crear">
</form>
<?php
// Cerramos la conexion
if ($con != null) {
$con->close();
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment