Skip to content

Instantly share code, notes, and snippets.

@isstaif
Created April 6, 2013 08:45
Show Gist options
  • Save isstaif/5325438 to your computer and use it in GitHub Desktop.
Save isstaif/5325438 to your computer and use it in GitHub Desktop.
Simple website using PHP and MySQL
<form action='newpost.php' method=post>
Title: <input type=text name=title />
Body: <input type=text name=body />
<input type=submit />
</form>
<?php
$con = mysqli_connect('localhost', 'root', '', 'db');
$query = "INSERT INTO posts (id, title, body) VALUES (NULL, '".$_POST['title']."', '". $_POST['body'] . "');";
$result = mysqli_query($con, $query);
echo "Thank you!";
header("location:posts.php");
?>
<html>
<head></head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'db');
$id= $_GET['id'];
$query = "SELECT * FROM `posts` WHERE id=". $id;
$result = mysqli_query($con, $query);
$post = mysqli_fetch_assoc($result);
echo "<h1>".$post['title']."</h1>";
echo "<p>".$post['body']."</p>";
echo "<hr />";
?>
<a href='posts.php'>Back</a>
</body>
</html>
<html>
<head></head>
<body>
<a href='new.html'>Create a new post!</a>
<?php
$con = mysqli_connect('localhost', 'root', '', 'db');
$result = mysqli_query($con, "SELECT * FROM `posts`");
while ($post = mysqli_fetch_assoc($result)){
$link = "<a href='post.php?id=".$post['id']. "'>";
echo $link;
echo "<h1>".$post['title']."</h1>";
echo "</a>";
echo "<p>".$post['body']."</p>";
echo "<hr />";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment