Originally Posted by
Corrderio
Well if you don't want to recreate the table for the new column, that's fine. Anyway I have to head to work soon, the gist of why an autoincrement is a good idea since your name field isn't a real primary key since it could be possible to have 2 games by the samn name in your database, but on different consoles.
So just make title + console the primary key?
So I started on the search. Main issue i'm having is seperating the php statments lol. When I try to enter a title into the search i get a error saying console, rarity, ext arent defined (basically its calling the add game to database button instead. This is what I have:
Code:
<div id="leftNav">
<form action = "searchResults.php" method = "post">
<h3>Search: </h3> <br>
Title: <input type="Text" name="title"><br>
<input type = "submit" value = "Submit" name = "search" style="width: 75px; height: 50px;" />
<?php
if (empty($_POST)) {
//No form data found, moving on
}
else {
//Creating the string for the SQL statement with named placeholders
$sql = "Select * FROM GAMES WHERE title = ':title'";
//Prepare the query
$sth = $dbh->prepare($sql);
//Create array with the named place holders as keys and form data as values
$data = array(
':title' => $_POST["title"],
);
//Execute the query with the place holders populated by the data from the array
$sth->execute($data);
}
?>
</form>
</div>
Also made a php site.
Full code for database.php:
Code:
<html>
<?php
// Specify your sqlite database name and path //
$dir = 'sqlite:GameCollection';
// Instantiate PDO connection object and failure msg //
$dbh = new PDO($dir) or die("cannot open database");
// Define your SQL statement, myTable = table name in your DB //
$sql = "SELECT * FROM Games";
// Apply statement //
$statement = $dbh->query($sql);
// Fetch the results //
$rowset = $statement->fetchAll();
?>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>NES Collection</title>
<link id="pagestyle" href="style.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<body>
<div id="header">
<h1>Retro Game Collection</h1>
</div>
<div id="leftNav">
<form action = "searchResults.php" method = "post">
<h3>Search: </h3> <br>
Title: <input type="Text" name="title"><br>
<input type = "submit" value = "Submit" name = "search" style="width: 75px; height: 50px;" />
<?php
if (empty($_POST)) {
//No form data found, moving on
}
else {
//Creating the string for the SQL statement with named placeholders
$sql = "Select * FROM GAMES WHERE title = ':title'";
//Prepare the query
$sth = $dbh->prepare($sql);
//Create array with the named place holders as keys and form data as values
$data = array(
':title' => $_POST["title"],
);
//Execute the query with the place holders populated by the data from the array
$sth->execute($data);
}
?>
</form>
</div>
<div id="body">
<h1>NES Titles</h1>
<!-- This controlles and conatins the 3 collumn text boxes -->
<div id="contentBox" style="margin:0px auto; width:100%">
<div id="column1" style="float:left">
content (scrolls overflow)
</div>
<div id="column2" style="float:left">
content (scrolls overflow)
</div>
<div id="column3" style="float:left;">
content (scrolls overflow)
</div>
</div>
<div id="LowerBody">
<table border ="1" width: "100%">
<tr>
<th>Title</th>
<th>Console</th>
<th>Genre</th>
<th>Publisher</th>
<th>Release Year</th>
<th>Rarity</th>
</tr>
<tr>
<?php foreach ( $rowset as $row ) : ?>
<td><?=$row['title']?></td>
<td><?=$row['console']?></td>
<td><?=$row['genre']?></td>
<td><?=$row['publisher']?></td>
<td><?=$row['releaseYear']?></td>
<td><?=$row['rarity']?></td></tr>
<?php endforeach; ?>
</table>
</div>
<div id="midBody">
<form action = "database.php" method = "post" enctype="multipart/form-data">
Add a Game to the database:<br>
<!-- Input field for the game title -->
Title: <input type="Text" name="title"><br>
<!-- Drop down box for the console -->
Console:
<select name = "console">
<option value =""> </option>
<option value ="Nintendo"> Nintendo </option>
<option value ="Super Nintendo"> Super Nintendo </option>
<option value ="Nintendo 64"> Nintendo 64 </option>
<option value ="Game Cube"> Game Cube </option>
<Option value ="Playstation"> Playstation </option>
<option Value ="Playstation2"> Playstaiton 2 </option>
<option value ="Xbox"> Xbox </option>
</select><br>
<!-- Drop down box for the Genre -->
Genre:
<select name= "genre">
<option value =""> </option>
<option value="Action"> Action </option>
<option value="Beatup"> Beat'em Up </option>
<option value="Platform"> Platformer </option>
<option value="shooter"> Shooter </option>
<option value="Adventure"> Adventure </option>
<option value="rpg"> Role-Playing </option>
<option value="Sim"> Simulation </option>
<option value="strategy"> Strategy </option>
<option value="Sports"> Sports </option>
</select><br>
<!-- Drop down box for the publisher -->
Publisher:
<select name = "publisher">
<option value =""> </option>
<option value="Aklaim"> Aklaim </option>
<option value="Capcom">Capcom </option>
<option value="EA"> EA </option>
<option value="KOEI">KOEI</option>
<option value="Konami"> Konami </option>
<option value="Nintendo"> Nintendo </option>
<option value ="Rare">Rare</option>
<option value="Snk">SNK</option>
<option value="SunSoft">SunSoft </option>
<option value="Square Enix"> Square Enix </option>
<option value="Tecmo"> Tecmo </option>
<option value="TradeWest"> TradeWest </option>
<option value="Broderbund"> Broderbund </option>
</select><br>
<!-- Text field for the release year -->
Release Year: <input type="Text" name="releaseYear"><br>
<!-- Drop down box for the Rarity option -->
Rarity:
<select name= "rarity">
<option value =""> </option>
<option value="Common"> Common </option>
<option value="Uncommon"> Uncommon </option>
<option value="Rare"> Rare </option>
<option value="veryRare"> Very Rare </option>
<option value="SuperRare"> Super Rare </option>
<option value="Ultrarare"> Ultra Rare </option>
</select><br>
Upload Image: <input type="file" name="uploadFile"><br>
<input type = "submit" value = "Submit" name = "gameSubmit" style="width: 100px; height: 50px;" />
<!-- php code to upload the image to the "image" folder -->
<?php
if(!empty($_FILES)) {
$target_dir = "images/";
$target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
$uploadOk=1;
// Check if file already exists
if (file_exists($target_dir . $_FILES["uploadFile"]["name"])) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
else {
if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<!-- PHP statments to push New game data to the database -->
<?php
if (empty($_POST)) {
//No form data found, moving on
}
else {
//Creating the string for the SQL statement with named placeholders
$sql = "Insert Into Games VALUES (:title, :console, :genre, :publisher, :releaseYear, :rarity)";
//Prepare the query
$sth = $dbh->prepare($sql);
//Create array with the named place holders as keys and form data as values
$data = array(
':title' => $_POST["title"],
':console' => $_POST["console"],
':genre' => $_POST["genre"],
':publisher' => $_POST["publisher"],
':releaseYear' => $_POST["releaseYear"],
':rarity' => $_POST["rarity"],
);
//Execute the query with the place holders populated by the data from the array
$sth->execute($data);
}
?>
</form>
</div>
</div>
</body>
</html>
Code for searchResults.php (way im displaying the search results)
Code:
<html>
<html>
<?php
// Specify your sqlite database name and path //
$dir = 'sqlite:GameCollection';
// Instantiate PDO connection object and failure msg //
$dbh = new PDO($dir) or die("cannot open database");
// Define your SQL statement, myTable = table name in your DB //
$sql = "";
// Apply statement //
$statement = $dbh->query($sql);
// Fetch the results //
$rowset = $statement->fetchAll();
?>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>NES Collection</title>
<link id="pagestyle" href="style.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<body>
<div id="header">
<h1>Retro Game Collection</h1>
</div>
<div id="leftNav">
<form action = "database.php" method = "post">
<h3>Search: </h3> <br>
Title: <input type="Text" name="title"><br>
<input type = "submit" value = "Submit" name = "gameSubmit" style="width: 75px; height: 50px;" />
<?php
if (empty($_POST)) {
//No form data found, moving on
}
else {
//Creating the string for the SQL statement with named placeholders
$sql = "Select * from Games where title = ':title'";
//Prepare the query
$sth = $dbh->prepare($sql);
//Create array with the named place holders as keys and form data as values
$data = array(
':title' => $_POST["title"],
);
//Execute the query with the place holders populated by the data from the array
$sth->execute($data);
}
?>
</form>
</div>
<div id="body">
<h1>NES Titles</h1>
<!-- This controlles and conatins the 3 collumn text boxes -->
<div id="contentBox" style="margin:0px auto; width:100%">
<div id="column1" style="float:left">
content (scrolls overflow)
</div>
<div id="column2" style="float:left">
content (scrolls overflow)
</div>
<div id="column3" style="float:left;">
content (scrolls overflow)
</div>
</div>
<div id="LowerBody">
<table border ="1" width: "100%">
<tr>
<th>Title</th>
<th>Console</th>
<th>Genre</th>
<th>Publisher</th>
<th>Release Year</th>
<th>Rarity</th>
</tr>
<tr>
<?php foreach ( $rowset as $row ) : ?>
<td><?=$row['title']?></td>
<td><?=$row['console']?></td>
<td><?=$row['genre']?></td>
<td><?=$row['publisher']?></td>
<td><?=$row['releaseYear']?></td>
<td><?=$row['rarity']?></td></tr>
<?php endforeach; ?>
</table>
</div>
</body>
</html>
And if for some reason a full zip of what I have would help here it is:
https://drive.google.com/file/d/0B1A...ew?usp=sharing