· 7 years ago · Sep 26, 2018, 09:00 PM
1<?php
2
3echo ' <link href="common/style.css" rel="stylesheet" type="text/css" />';
4echo "<body onLoad='document.getElementById('song').select();'>";
5//create a PDO object to interface with the database
6try {
7 $dbh = new PDO('mysql:localhost:3306', 'root', '');
8}
9catch (PDOException $e) {
10 echo 'Connection failed: ' . $e->getMessage();
11}
12/*
13//create the database
14try{
15 $dbh->exec("CREATE DATABASE IF NOT EXISTS isp5;");
16}
17catch (PDOException $e) {
18 die("Creating Database Error: ". $e->getMessage());
19}
20
21//create the songs table
22try{
23 $dbh->exec("USE isp5;");
24 $dbh->exec("CREATE TABLE IF NOT EXISTS songs ( song VARCHAR(40) NOT NULL, artist VARCHAR(40), votes INT(10) NOT NULL, PRIMARY KEY (song));");
25}
26catch (PDOException $e) {
27 die("Creating Table Error: ". $e->getMessage());
28}
29
30//put a few test songs in the database
31try{
32 $dbh->exec("USE isp5;");
33 $dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song1', 'artist1', '0');");
34 $dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song2', 'artist1', '0');");
35 $dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('song3', 'artist2', '0');");
36}
37catch (PDOException $e) {
38 die("Adding Test Entries: ". $e->getMessage());
39}*/
40
41//create the songs table
42
43
44
45try{
46 $dbh->exec("USE isp5;");
47 $order = 'song';
48 $sth = $dbh->prepare("SELECT * FROM songs ORDER BY $order");
49 $sth->execute();
50 $result = $sth->fetchAll(PDO::FETCH_ASSOC);
51
52 echo "<form action='' method='POST'>";
53 echo "<table><tr><th><a href=''>Song</a></th><th><a href=''>Artist</a></th><th><a href=''>Votes</a></th><th></th></tr>";
54 foreach($result as $r){
55 echo "<tr><td>".$r[song]."</td><td>".$r[artist]."</td><td>".$r[votes]."</td>";
56 echo "<td><a href='?up=".$r[song]."' name=up id='".$r[song]."'><img src='common/up.png' /></a>";
57 echo "<a href='?down=".$r[song]."' name=down id='".$r[song]."'><img src='common/down.png' /></a></td></tr>";
58 }
59 echo "</table>";
60}
61catch (PDOException $e) {
62 die("Printing Data Error: ". $e->getMessage());
63}
64
65echo"
66<div id='new'>
67<form action='' method='POST'>
68<fieldset style='width:200px'>
69<legend> Song </legend>
70<input type='text' id='song' name='song' value='' />
71<legend> Artist </legend>
72<input type='text' id='artist' name='artist' value='' />
73<input type='submit' name='add' value='add' /></div>
74";
75
76
77if (isset($_POST['add']))
78{
79 try{
80 $dbh->exec("USE isp5;");
81 $song = $_POST['song'];
82 $artist = $_POST['artist'];
83 $dbh->exec("INSERT INTO songs(song, artist, votes) VALUES('$song', '$artist', '1');");
84 echo '<script type="text/javascript"> window.location.reload() </script>';
85 }
86 catch (PDOException $e) {
87 die("Adding Test Entries: ". $e->getMessage());
88 }
89}
90
91if (isset($_GET['up'])){
92 // echo "<script type='text/javascript'> alert('werwer'); </script>";
93 try{
94 $dbh->exec("USE isp5;");
95 $song = $_GET['up'];
96 $sth = $dbh->prepare("SELECT votes FROM songs WHERE song='$song';");
97 $sth->execute();
98 $result = $sth->fetchAll(PDO::FETCH_ASSOC);
99 $votes = $result[0][votes];
100 $votes++;
101 echo ' votes: '.$votes;
102 $dbh->exec("UPDATE songs SET votes=$votes WHERE song='$song';");
103 header('location:');
104 }
105 catch (PDOException $e) {
106 die("Adding Test Entries: ". $e->getMessage());
107 }
108}
109
110
111if (isset($_GET['down'])){
112 //echo "<script type='text/javascript'> alert('werwer'); </script>";
113 try{
114 $dbh->exec("USE isp5;");
115 $song = $_GET['down'];
116
117 $sth = $dbh->prepare("SELECT votes FROM songs WHERE song='$song';");
118 $sth->execute();
119 $result = $sth->fetchAll(PDO::FETCH_ASSOC);
120 $votes = $result[0][votes];
121 $votes--;
122 echo ' votes: '.$votes;
123 $dbh->exec("UPDATE songs SET votes=$votes WHERE song='$song';");
124 header('location:');
125 }
126 catch (PDOException $e) {
127 die("Adding Test Entries: ". $e->getMessage());
128 }
129}
130
131
132
133?>