-
-
Save evanpurkhiser/2374339 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
<?php | |
echo ' <link href="common/style.css" rel="stylesheet" type="text/css" />'; | |
echo "<body onLoad='document.getElementById('song').select();'>"; | |
//create a PDO object to interface with the database | |
try { | |
$dbh = new PDO('mysql:localhost:3306', 'root', ''); | |
} | |
catch (PDOException $e) { | |
echo 'Connection failed: ' . $e->getMessage(); | |
} | |
/* | |
//create the database | |
try{ | |
$dbh->exec("CREATE DATABASE IF NOT EXISTS isp5;"); | |
} | |
catch (PDOException $e) { | |
die("Creating Database Error: ". $e->getMessage()); | |
} | |
//create the songs table | |
try{ | |
$dbh->exec("USE isp5;"); | |
$dbh->exec("CREATE TABLE IF NOT EXISTS songs ( song VARCHAR(40) NOT NULL, artist VARCHAR(40), votes INT(10) NOT NULL, PRIMARY KEY (song));"); | |
} | |
catch (PDOException $e) { | |
die("Creating Table Error: ". $e->getMessage()); | |
} | |
//put a few test songs in the database | |
try{ | |
$dbh->exec("USE isp5;"); | |
$dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song1', 'artist1', '0');"); | |
$dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song2', 'artist1', '0');"); | |
$dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song3', 'artist2', '0');"); | |
} | |
catch (PDOException $e) { | |
die("Adding Test Entries: ". $e->getMessage()); | |
}*/ | |
//create the songs table | |
$dbh->exec("USE isp5;"); | |
if (isset($_POST['add'])) | |
{ | |
try{ | |
$dbh->exec("USE isp5;"); | |
$song = $_POST['song']; | |
$artist = $_POST['artist']; | |
$dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('$song', '$artist', '1');"); | |
} | |
catch (PDOException $e) { | |
die("Adding Test Entries: ". $e->getMessage()); | |
} | |
} | |
if (isset($_GET['up'])){ | |
// echo "<script type='text/javascript'> alert('werwer'); </script>"; | |
try{ | |
$dbh->exec("USE isp5;"); | |
$song = $_GET['up']; | |
$sth = $dbh->prepare("SELECT votes FROM songs WHERE song='$song';"); | |
$sth->execute(); | |
$result = $sth->fetchAll(PDO::FETCH_ASSOC); | |
$votes = $result[0][votes]; | |
$votes++; | |
echo ' votes: '.$votes; | |
$dbh->exec("UPDATE songs SET votes=$votes WHERE song='$song';"); | |
} | |
catch (PDOException $e) { | |
die("Adding Test Entries: ". $e->getMessage()); | |
} | |
} | |
if (isset($_GET['down'])){ | |
//echo "<script type='text/javascript'> alert('werwer'); </script>"; | |
try{ | |
$dbh->exec("USE isp5;"); | |
$song = $_GET['down']; | |
$sth = $dbh->prepare("SELECT votes FROM songs WHERE song='$song';"); | |
$sth->execute(); | |
$result = $sth->fetchAll(PDO::FETCH_ASSOC); | |
$votes = $result[0][votes]; | |
$votes--; | |
echo ' votes: '.$votes; | |
$dbh->exec("UPDATE songs SET votes=$votes WHERE song='$song';"); | |
} | |
catch (PDOException $e) { | |
die("Adding Test Entries: ". $e->getMessage()); | |
} | |
} | |
try{ | |
$order = 'song'; | |
$sth = $dbh->prepare("SELECT * FROM songs ORDER BY $order"); | |
$sth->execute(); | |
$result = $sth->fetchAll(PDO::FETCH_ASSOC); | |
echo "<form action='' method='POST'>"; | |
echo "<table><tr><th><a href=''>Song</a></th><th><a href=''>Artist</a></th><th><a href=''>Votes</a></th><th></th></tr>"; | |
foreach($result as $r){ | |
echo "<tr><td>".$r[song]."</td><td>".$r[artist]."</td><td>".$r[votes]."</td>"; | |
echo "<td><a href='?up=".$r[song]."' name=up id='".$r[song]."'><img src='common/up.png' /></a>"; | |
echo "<a href='?down=".$r[song]."' name=down id='".$r[song]."'><img src='common/down.png' /></a></td></tr>"; | |
} | |
echo "</table>"; | |
} | |
catch (PDOException $e) { | |
die("Printing Data Error: ". $e->getMessage()); | |
} | |
echo" | |
<div id='new'> | |
<form action='' method='POST'> | |
<fieldset style='width:200px'> | |
<legend> Song </legend> | |
<input type='text' id='song' name='song' value='' /> | |
<legend> Artist </legend> | |
<input type='text' id='artist' name='artist' value='' /> | |
<input type='submit' name='add' value='add' /></div> | |
"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment