Last active
February 1, 2018 15:55
-
-
Save jamezrin/33b8862ff5b150e991aaa52bc254adc4 to your computer and use it in GitHub Desktop.
This file contains 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
<!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