· 7 years ago · Sep 26, 2018, 08:52 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 $votes = $dbh->exec("SELECT votes FROM songs WHERE song='$song'");
97 echo 'song: '.$song;
98 echo ' votes: '.$votes;
99 $votes++;
100 echo ' votes: '.$votes;
101 $dbh->exec("UPDATE songs SET votes='$votes' WHERE song='$song');");
102 }
103 catch (PDOException $e) {
104 die("Adding Test Entries: ". $e->getMessage());
105 }
106}
107
108
109if (isset($_GET['down'])){
110 //echo "<script type='text/javascript'> alert('werwer'); </script>";
111 try{
112 $dbh->exec("USE isp5;");
113 $song = $_GET['down'];
114 $votes = $dbh->exec("SELECT votes FROM songs WHERE song='$song';");
115 print_r($votes);
116 echo 'song: '.$song;
117 echo ' votes: '.$votes[votes];
118 $votes--;
119 echo ' votes: '.$votes;
120 $dbh->exec("UPDATE songs SET votes='$votes' WHERE song='$song');");
121 }
122 catch (PDOException $e) {
123 die("Adding Test Entries: ". $e->getMessage());
124 }
125}
126
127
128
129?>