<?php
include("credentials.php");
?>
<html>
<head>
<title>Week #15 Basic ~ Lestat</title>
<link rel="stylesheet" type="text/css" href="/css/scriptskewl.css">
<style type="text/css">
table{border-collapse: collapse; margin:0; padding:0; border: 1px solid #000;}
td{margin:0; padding:0; border: 1px solid #000;}
form{margin: 0; padding:0;}
fieldset{margin-bottom: 1em;}
</style>
</head>
<body>
<?php
$db = 'scriptskewl';
$mysql_access = mysql_connect("localhost" , $user , $pw); // Connection String
if(!$mysql_access){
print("No Connection!<br />");
}
mysql_select_db($db, $mysql_access); // Select the db
/***********************
* $_POST Vars
***********************/
$artist_name = $_POST['artist_name'];
$genre = $_POST['genre'];
$selectVote = $_POST['selectVote'];
$nameVote = $_POST['nameVote'];
/********************
* Post a Vote
********************/
if(isset($_POST['selectVote'])){
print("<p>You Voted for : <b>$nameVote</b></p>");
$query = "UPDATE music_artists SET votes = votes + 1 WHERE ID = $selectVote";
if(!$result = mysql_query($query)){
print("<b><font color=\"red\"> ERROR: $query " . mysql_error() . "</font></b>");
}
}
/***********************
* Insert Form Data
***********************/
if(isset($_POST['addArtist'])){
if(($_POST['artist_name'] != '') && ($_POST['genre'] != '')){
$query = "INSERT INTO music_artists VALUES('' , '$artist_name' , '$genre' , '')"; // assign querry
mysql_query($query);
Print("<p>Added <b>$_POST[artist_name]</b> , <b>$_POST[genre]</b></p><br />");
} else {
print("<p><font color=\"red\">Incomplete data. Please fill in both fields.</font></p>");
}
}
?>
<fieldset style="width:50em;">
<legend>Add An Artist</legend>
<form method="post" action="<?php $PHP_SELF ?>">
Artist Name: <input type="text" name="artist_name" />
Genre: <select name="genre">
<option></option>
<option>Hard Rock</option>
<option>Soft Rock</option>
<option>Blues</option>
<option>Just Plain Gay</option>
</select>
<input type="submit" name="addArtist" value="Submit" />
</form>
</fieldset>
<?php
/********************
* Display Results
*********************/
print("
<table>
<tr>
<td>ID</td><td><b>Artist</b></td><td><b>Genre</b></td><td><b>Votes</b></td>
</tr>
");
$query = "SELECT * FROM music_artists ORDER BY ID"; // What data are we looking for? (SELECT everything FROM music_artists table)
$results = mysql_query($query); // Assign results to variable
if(!$results){
print("<b>Invalid Query:</b> " . mysql_error() . "<br /><b>Whole Query:</b> " . $query . "<br />");
}
$totalVotes = 0;
while($row = mysql_fetch_array($results, MYSQL_ASSOC)){ // Print out results
$totalVotes = $totalVotes + $row['votes'];
print("<tr>
<td>$row[ID]</td>
<td>$row[artist_name]</td>
<td>$row[genre]</td>
<td>$row[votes]</td>
<td> <form method=\"post\" action=\"$PHP_SELF\">
<input type=\"hidden\" name=\"selectVote\" value=\"$row[ID]\" />
<input type=\"hidden\" name=\"nameVote\" value=\"$row[artist_name]\" />
<input type=\"submit\" name=\"voteButton\" value=\"Vote\" /></form></td></tr>
");
}
print("</table>");
print("Total Votes: $totalVotes<br />");
?>
<!------------- END ------------->
<br /><a href='../index.html'>Index</a><br />
<iframe src='wk15basicsrc.php' width='100%' height='210' style="margin:0;"></iframe>
<a href='../index.html'>Index</a> <a href="http://www.timslan.com/scriptskewl/admin.php">....</a>
</body>
</html>