Created
April 13, 2012 05:23
-
-
Save RSquaredSoftware/2373918 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 | |
try{ | |
$dbh->exec("USE isp5;"); | |
$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='#' name=up id=.".$r[song]." onclick='document.SubmitUsers.submit();return false;'> | |
<img src='common/up.png' /></a>"; | |
echo "<a href='#' name=down id=.".$r[song]." onclick='document.SubmitUsers.submit();return false;'> | |
<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> | |
"; | |
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');"); | |
echo '<script type="text/javascript"> window.location.reload() </script>'; | |
} | |
catch (PDOException $e) { | |
die("Adding Test Entries: ". $e->getMessage()); | |
} | |
} | |
if (isset($_POST['up'])){ | |
echo "<script type='text/javascript'> alert('werwer'); </script>"; | |
try{ | |
$dbh->exec("USE isp5;"); | |
$song = $_POST['song']; | |
$votes = $dbh->exec("SELECT votes FROM songs WHERE song='$song';"); | |
echo 'votes: '.$votes; | |
$votes++; | |
$dbh->exec("UPDATE songs SET votes='$votes' WHERE song='$song');"); | |
} | |
catch (PDOException $e) { | |
die("Adding Test Entries: ". $e->getMessage()); | |
} | |
} | |
if (isset($_POST['down'])){ | |
try{ | |
$dbh->exec("USE isp5;"); | |
$votes=$dbh->exec("SELECT votes FROM songs WHERE song=$song"); | |
$votes--; | |
$dbh->exec("UPDATE songs SET votes=$votes"); | |
echo '<script type="text/javascript"> window.location.reload() </script>'; | |
} | |
catch (PDOException $e) { | |
die("Printing Data Error: ". $e->getMessage()); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment