Creating a (simple) flash game website with rating system - php

He guys,
For school I need to make a website where you can play flash games,
rate games by leaving reactions in a text form and a vote system which uses a number system (i.e. 1 = extremely bad and 10 = very good.).
Right now what I want to do is this:
Have an index page for each category of games where users can click on a games name and be directed to another page where the script loads the game.
So far I've written this code for the index (master) page.
<!DOCTYPE html>
<?php
include("dbconnect.php");
?>
<html>
<head>
<meta charset="UTF-8">
<title>Master page</title>
</head>
<body>
<?php
//Place all data from this mySQL query in $result.
$result = mysql_query("SELECT * FROM gamesDB");
//While a row of data exists, put that row in $data as an associative array.
while($data = mysql_fetch_assoc($result)) {
//Echo a link to all the games in the MySQL database.
echo "<a href='detail.php?id=" . $data['ID'] . "'>";
//Echo the games name in the url.
echo $data['Spel'];
//Echo the closing tags
echo "</a>";
echo "<br />";
}
?>
</body>
</html>
And this is for the game (detail) page.
<!DOCTYPE html>
<?php
include("dbconnect.php");
?>
<html>
<head>
<meta charset="UTF-8">
<title>Detail page</title>
</head>
<body>
<?php
//Place all data out of the database, with the ID number retrieved out of the url into $result.
$result = mysql_query("SELECT * FROM gamesDB WHERE id = '" . $_GET['id'] . "'");
//While a row of data exists, put that row in $data as an associative array.
while($data = mysql_fetch_assoc($result)) {
//Retrieve the files name from the database and place it in the <embed> tags as src="...".
echo "<embed width='800' height='512' src='" . $data['file'] . "' type='application/x-shockwave-flash'></embed>";
//Echo the games name
echo "Spel: " . $data['Spel'] . "<br />";
//Echo the points (not yet functional)
echo "Punten: " . $data['Punten'] . "<br />";
//Echo all reactions from users regarding this game.
echo "Reacties: " . $data['Reactie'] . "<br />";
}
?>
</body>
</html>
When I click on the link in the masterpage I get redirected to the detail page but unfortunately, the game does not load.
In my MySQL DB I added the file name to the first row with ID 1. I thought, when I inquire for the filename in the tags it would load the game but it says (when I right click the box in which the game should display) "Movie not loaded...".
Can anybody help me get this to work ? Is my thinking way off perhaps, or am I headed in the right direction.
Since it is an assignment for school, there is no need to worry about any SQL injection vulnerabilities.
Thanks!

I actually forgot to put an entry into the file column.
Now that the problem I had with "<embed>" has been resolved, I would like to focus on how to add user comments to my 'comments' column, and have each comment displayed. I'd like to find the code myself as much as possible so you could just react to my question with pointers instead of writing the complete code I would be very grateful.

Related

Show Posts from Only One User

hello I have this table in my database called posts:
1-id 2-poster 3-Title 4-date 5-hour 6-imagem 7-desc
then I created a post:
id - 1 poster - Gary Title- What is the concept of machine learning?
date - 2021/05/19 hour - 4:32 PM imagem
-https://www.iberdrola.com/wcorp/gc/prod/pt_BR/comunicacion/machine_learning_mult_1_res/machine_learning_746x419.jpg
desc-Machine learning (in English, machine learning) is a method of
data analysis that automates the construction of analytical models. It
is a branch of artificial intelligence based on the idea that systems
can learn from data, identify patterns and make decisions with minimal
human intervention.
then I created a php file called Index.html that does an encoding in Pdo:
<?php
include_once 'con.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<?php
$result_msg_cont = "SELECT * FROM posts ORDER BY id Desc";
$resultado_msg_cont = $conn->prepare($result_msg_cont);
$resultado_msg_cont->execute();
while ($row_msg_cont = $resultado_msg_cont->fetch(PDO::FETCH_ASSOC)) {
$post_id = $row_msg_cont['poster'];
$Post_Title = $row_msg_cont['Title'];
$data = $row_msg_cont['date'];
$hora = $row_msg_cont['hour'];
$desc = $row_msg_cont['desc'];
$imagem = $row_msg_cont['imagem'];
echo "<br><p> Posted in " . $data . " at " . $hora."</p><br>";
echo "<h2> $Post_Title</h2><br>";
echo "<img src='" .$imagem. "' class='img_posts'><br>";
echo " <br><h4 class='posts_desc'> " . $desc . "</h4><br>";
echo "poster: " . $post_id. "<br><br><hr>";
}
?>
</body>
</html>
the result was great because it really displays the Posts, but I only wanted to display the posts of just one example user:
I just want to show Gary posts, if in case another user with another
name example Fred if Fred posts something the post doesn't appear in
index.php
is there any way to do this?
Short Answer :
You will need to use where clause in query.
e.g.
$result_msg_cont = "SELECT * FROM posts where poster= 'Gary' ORDER BY id Desc";
Long Answer :
Add poster_unique_id column in your posts table.
create one more database table named users with columns like id, unique_id, name, user_status, showhide. Use unique random string for unique_id column for each poster details.
Now display names of users (posters) with link to them with GET value.
e.g.
<?php echo $userdata['name']:?>
// HERE USER NAME AND UNIQUEID IS FETCHED FROM users TABLE
Then in your above current code, add
$poster = $_GET['poster'];
// then use query like
$result_msg_cont = "SELECT * FROM posts where poster_unique_id = '$poster' ORDER BY id Desc";
** Data sanitization etc not considered in this example.
I didn't get your this line - then I created a php file called Index.html
is it index.php or index.html ??

How do I send a variable via GET Request between 2 php pages

In my program, I have a table Artists in my database with an ID, name, and gender. I am trying to create 2 PHP pages. The first page prints the name of all artists and their genders. I want to hyperlink every name on the first page, to the second page. So whenever I click the artist, the ID is sent to the second page.I will use the ID to compare to another table to print out some other information.I am trying to perform the above procedure using GET. However, my code isn't working. The value I am trying to send is row[artist_id] ie, $id.
First PHP Page
......
$sql = 'SELECT name, gender,artist_id FROM artists '
. ' ORDER BY name ASC, artist_id ASC';
$result = $pdo->query($sql);
echo "<table>";
echo "<tr><th>Artist name</th><th>Gender</th></tr>";
foreach ($result as $row) {
echo "<tr>";
$name = htmlspecialchars($row['name']);
$gender = htmlspecialchars($row['gender']);
$id = $row['artist_id'];
echo "<td><a href='artist_events.php'?val=$id>".$name."</a></td>";
echo"<td>".$gender."</td>";
echo "</tr>";
Second PHP Page
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
table,th,td{
border: 1px solid black;
}
</style>
<title>My second PHP page</title>
</head>
<body>
<?php
include 'config.php';
?>
<?php
$my_id= $_GET['val'];
echo $my_id;
?>
</body>
echo "<td><a href='artist_events.php'?val=$id>".$name."</a></td>";
The issue is with this line. You have closed the href attribute before passing the GET parameter.
Change it to
echo "<td><a href='artist_events.php?val=$id'>".$name."</a></td>";
I have changed the position of closing quotes for href attribute.

Can't display table contents that contain URL's from database

I'm a student using NetBeans to create very basic webpage(s) using HTML, PHP and SQLite. So far, everything is fine. The problem I have is that images aren't displayed on the moviedetails.php page. Everything else including the titles, ratings and description for each table entry works fine. (I am retrieving rows from a database table.) Here is my code:
(This is very new to me, so if it's a simple mistake, sorry for wasting your time :/)
Index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$pdo = new PDO('sqlite:movies.db'); //Import SQLite database "movies.db" to a Var
$query = $pdo->query("SELECT * FROM movie");
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
//For each id number in db, echo a hyperlink containing that ID's title and
echo '' . htmlentities($row['title']) . '';
echo '<br>';
}
?>
</body>
moviedetails.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$pdo = new PDO('sqlite:movies.db'); //Using movies.db
$query = $pdo->prepare("SELECT * FROM movie WHERE id=:id"); //Prepare this statement
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); //GET INPUT from Variable 'id' and FILTER anything which isn't a number
$query->bindParam(':id', $id, PDO::PARAM_INT); //Bind :name 'id' to a $id variable
$query->execute(); //Execute the prepared statement
$row = $query->fetch(PDO::FETCH_ASSOC); //Fetch next row of results
//var_dump($row);
//display title, description and rating
echo '<h1>'.htmlentities($row['title']).'</h1>'; //Echo 'Title' from db into a heading
echo ''; //Echo 'image from db into a link
echo '<p>'.htmlentities($row['description']).'</p>'; //Echo 'description' from db to paragraph
echo '<p>Rating: '. htmlentities($row['rating']).'</p>'; //Echo 'rating' from db to paragraph
?>
</body>
Here is my database in an image, as this is the easiest way to show you:
http://i.cubeupload.com/TBI5Fv.png
Here is one of the webpages that should diplay a link. However, it contains only the other table fields:
http://i.cubeupload.com/1tcfsU.png
The strange thing is, it doesn't give me any errors, so I don't know where I'm going wrong.
Hope someone can help :)
Your <a> tag is empty, so it's invisible.
echo '';
You should put some content that will be displayed as a link like this:
echo 'THIS IS LINK TO IMAGE';
If you want to display the image itself instead of a link, you should use <img> tag like this:
echo '<img src="'.htmlentities($row['image']).'"/>';

Open new page with table Data from previous page

I'm building a website for the company I work for that is going to eventually replace the CRM we are using. Right now I have created a simple php file that creates a table of simple/basic values (I'm trying to test the concept before I scale it up to the read deal). I am using a WHILE loop to generate the table by pulling data from the server. I was able to make the first column in each row into a clickable link that will open to a new page. On this page, I want to post more detailed data that can be edited. For example, in the display.php file that shows the table created with the while loop I will have a property address, a city name and a person who is working to either buy or sell that property. When the user clicks on the first address, I want it to open into a page that will display information like bedrooms, bathrooms, square footage, subdivision, asking price, etc. It would be nice to have each of those fields editable too, but that's a different thing to tackle. Right now, I'm concerned with being able to click on one property and have it open up with the correct data in the next page. Right now, it successfully opens the page but it only shows the data from the first row no matter which row I click on in the table.
Here is the code that I have for the page that displays the table:
<?php
session_start();
if(isset($_SESSION['id'])) {
$username = $_SESSION['username'];
$userId = $_SESSION['id'];
} else {
header('Location: index.php');
die();
}
$dbCon = mysqli_connect("localhost", "##", "##", "##");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect: " . mysqli_connect_error();
}
$sql="SELECT * FROM leads";
$records=mysqli_query($dbCon,$sql);
?>
<html>
<head>
<title>Display Data</title>
<body>
<table action="" method="post" class="table" id="example">
<tr>
<th>Address</th>
<th>City</th>
<tr>
<?php
while($leads=mysqli_fetch_assoc($records)){
echo "<tr>";
//I'm trying to figure out how to pass the record's ID as a way of keeping track of which record I want to look at when I open it in the next page. I don't know how to put the id in the link so that it carries through to the next page.
echo "<td><a href='showstuff.php?leadid=$leadid'>".$leads['address']."</a></td>";
echo "<td>".$leads['city']."</td>";
echo "</tr>";
}
?>
</tr>
</table>
</body>
</head>
</html>
Here is my code for the page that should open up with a more detailed "profile view" of the data:
<?php
//connect to db
$dbCon = mysqli_connect("localhost", "##", "##", "##");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect: " . mysqli_connect_error();
}
//I tried to incorperate "WHERE leadid = '$_GET['leadid']'" in line 10 to define the lead id that is associated with the record that was selected in display.php and shoould be opened/shared to this page
$sql="SELECT * FROM leads";
$records=mysqli_query($dbCon,$sql);
$leads=mysqli_fetch_assoc($records);
//I tried to create a $_GET variable that calls on the record id that I am trying to pass from the display.php file to this page
$leadid=$_GET['leadid'];
?>
<html>
<head>
<title>Show Stuff</title>
<body>
<h1>Show Stuff Here</h1><br>
<?php
echo "<p>Test</p><br>";
//I only have one piece of information here to test if it works in the first place. Once it does, I'll add the rest of the fields to display
echo "Is ".$leadid=$leads['leadid']." your ID number?";
?>
</body>
</head>
</html>
Lastly, I am using sessions on here since eventually there will be various users with different levels of access to view and edit things. For now, it's really not too functional other than to log in and log out. It's also not very secure. But I'm more focused on figuring out the mechanics of one thing at a time as I build. It's no where near ready to be used. But I'm hopeful that I'll get there soon. You'll have to excuse my simple code, I'm just learning and teaching myself on my spare time since our company refuses to hire someone to actually do this...
You're putting $leadid in the URL parameters in the table, but you never set this variable.
<?php
while($leads=mysqli_fetch_assoc($records)){
echo "<tr>";
$leadid = $leads['leadid'];
echo "<td><a href='showstuff.php?leadid=$leadid'>".$leads['address']."</a></td>";
echo "<td>".$leads['city']."</td>";
echo "</tr>";
}
?>
Then you should be able to use $_GET['leadid'] in showstuff.php to show the information for the lead that they clicked on.
$leadid = intval($_GET['leadid']);
$sql="SELECT * FROM leads WHERE leadid = $leadid";
First, in your table, you have to put like this:
while($leads=mysqli_fetch_assoc($records)){
echo '<tr>';
echo '<td><a href="showstuff.php?leadid='.$leads['id'].'" >'.$leads['address'].'</a></td>';
echo '<td>'.$leads['city'].'</td>';
echo '</tr>';
}
Like this your link leadid will have a value.
Use simple quote ' ' for html value, it's faster and better for PHP. ;)
In your second file, make like this:
$leadid=$_GET['leadid'];
$sql="SELECT * FROM `leads` WHERE `id`='$leadid' ";
$records=mysqli_query($dbCon,$sql);
$leads=mysqli_fetch_assoc($records);
It's better to put for the table and columns names and ' ' for values.
In $leads array, you 'll have all data.
You can "print" it like this:
echo 'The ID is '.$leads['id'].' and the name is '$leads['name'];
I hope it's useful!

PHP and secure forms

I am doing an exercise from the book PHP & MYSQL in easy steps. It involves an HTML form to update a row in a database then various PHP scripts to check the the input data for HTML code and make it into a secure format. However, the code just does not work the way the book says. I went to the publisher's website and downloaded the code example, but no joy.
Instead of a form with the name of the row below it, instead I get the form, then below that "No valid new name submitted". Then below that the current name of row in the table which I want to change. When I try to enter and submit data into the form it makes no difference. It displays exactly the same page. The code is below.
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ensuring security
</title>
</head>
<body>
<form action="secure.php" method="POST">
<p>New Name : <input type="text" name="name">
<input type="submit"></p></form>
<?php
require('../connect_db.php');
if (!empty($POST['name']) && !is_numeric($_POST['name'])) {
$name = $POST['name'];
$name = mysqli_real_escape_string($dbc, $name);
$name = strip_tags($name);
$q = 'UPDATE towels SET name "' . $name . '" WHERE id= 1';
mysqli_query($dbc, $q);
} else {
echo 'No valid new name submitted';
}
$q = 'SELECT * FROM towels WHERE id = 1 ';
$r = mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
echo "<p>Name : $row[1] </p>";
}
mysqli_close($dbc);
I'd appreciate any ideas on this. I have spent about 3 hours and been on the publishers website, but I am still at square one.
There is no superglobal array $POST so you have to change $POST['name'] to $_POST['name'].
PHP can't see that array so it evaluates !empty($POST['name']) as false and never executes code with update query.
And, like #BartFriederichs said, buy better book. I don't think you'll learn something valuable from current one.

Categories