Skip to content

Instantly share code, notes, and snippets.

@camh96
Forked from Danni3/dbconnect.php
Last active August 29, 2015 14:05
Show Gist options
  • Save camh96/99dc343a48d3f4339104 to your computer and use it in GitHub Desktop.
Save camh96/99dc343a48d3f4339104 to your computer and use it in GitHub Desktop.
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "assessment";
$con = mysqli_connect($hostname, $username, $password, $dbname);
// check connection
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql="INSERT INTO news (Cipher, Date_published, Title, Notice, Department, Status)
VALUES
('$_POST[Cipher]','$_POST[Date_published]','$_POST[Title]','$_POST[Notice]','$_POST[Department]','$_POST[Status]')";
if (!mysqli_query($con,$sql))
{
die('ERROR: ' . mysqli_error($con));
}
echo "<h2>News item pending approval!</h2>";
// include redirect to back to homepage with success message
mysqli_close($con);
?>
<!doctype HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add to TC News</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.4.2/pure-min.css">
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<style>
body {
width: 1600px;
margin-right: auto;
margin-left: auto;
background: #FFF;
padding: 20px 30px 20px 30px;
font: 12px "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #888;
text-shadow: 1px 1px 1px #FFF;
padding-left: 25%;
}
h1 {
padding-left: 260px;}
</style>
</head>
<body>
<form method="post" action="dbconnect.php" class="pure-form">
<h1>Add some info here</h1>
<a href="newspage.php" class="button"><h2>Click here to go home</h2></a>
<fieldset class="pure-group">
<br><h3>Teacher Cipher</h3><input type="text" name="Cipher" size="30" class="pure-input-1-2" placeholder="TeacherCipher" />
<br><h3>Date Published</h3><input type="date" name="Date_published" size="30" class="pure-input-1-2" placeholder="DatePublished" />
<br><h3>Title</h3><input type="text" name="Title" size="50" class="pure-input-1-2" placeholder="Title" />
<br><h3>Message</h3><input type="textarea" name="Notice" size="2500" class="pure-input-1-2" placeholder="Message in here" />
<br><h3>Department</h3></br><select name ="Department">
<option>DIT</option>
<option>Science</option>
<option>Art</option>
<option>Food</option>
<option>Maths</option>
<option>English</option>
<option>Languages</option></select>
<br><h3>Status</h3><input type="text" name="Status" value="0" size="30" class="pure-input-1-2" placeholder="Status" readonly/>
</fieldset>
<input type="submit" value="Submit" class="pure-button pure-input-1-2 pure-button-primary" />
</form>
</body>
</html>
SELECT * FROM a LEFT JOIN b ON a.id = b.fkey
<?php
// login as root
$username = "root";
$password = "";
$hostname = "localhost";
//connect
$con = mysqli_connect($hostname, $username, $password, 'assessment');
// check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//mysql_select_db("news", $con);
$sql = <<<EOS
SELECT Cipher, Date_published, Title, Notice
FROM news
LIMIT 3
EOS;
$result = mysqli_query($con, $sql);
?>
<!doctype html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TC News homepage</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<style>
body
{
padding-left:25%;
}
</style>
</head>
<div class="row">
<div class="large-12 columns">
<div class=pure-button>
<a href="#" class="button">Home</a>
</div>
<br>
<div class=pure-button>
<br>
<a href="form2.php" class="button">Submit a post</a>
</div>
</div>
<h1><small>This is the TC News database.</small></h1>
</div>
</div>
</header>
<!-- End Nav -->
<?php
while($row = mysqli_fetch_array($result)) {
print "<h3>" . $row['Title'] . " </h3>";
print "<h6> Written by " . $row['Cipher'] . " </h6>";
print "<p>" . $row['Notice'] . " </p>";
print "<p>" . $row['Date_published'] . "</p> ";
// php convert date/time
}
?>:
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment