Jump to content

Recommended Posts

Hi Guys

 

I have the following code which display 'game' data from my database in the relevent html tags.

 

<?php
//get results from db

$sql = "SELECT * FROM Games ORDER BY gametitle";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0) die("No records found");

// loop through the results returned by your query
while($data = mysql_fetch_assoc($res))
{
    $title=$data['gametitle'];
$cover=$data['cover'];
$gameid=$data['gameid'];

// directory for images
$dir="coverart";
?>



<div id="cardcontainer">

<div id="coverart">
<?php echo "<img src='$dir/{$cover}' width='100' height='140'><br>"; ?>
</div>
<div id="gametitle">
<a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a>
</div>
<div id="friendrating"></div>
<div id="globalrating"></div>

</div>
<?php
}
?>

 

This works fine, apart from the

<a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a>

bit.

 

What i actually want this to do is to open more details of the individual game that the link is for. How can i do this?

Link to comment
https://forums.phpfreaks.com/topic/242267-help-with/
Share on other sites

<?php
//get results from db

if (isset($_GET['gameid']) && is_numeric($_GET['gameid'])) { // check to see is the gameis is both set and a number (Assuming gameid is an int in your DB
$gameid = mysql_real_escape_string($_GET['gameid'])); // added security
$sql = "SELECT * FROM Games WHERE gameid = $gameid"; // making the query, setting the where clause to only fetch the row with gameid of the selected int
$res = mysql_query($sql);
$data = mysql_fetch_assoc($res);

// However you'd like to format the html to output
// I'm assuming that the file you have put here is called review.php

} else {

$sql = "SELECT * FROM Games ORDER BY gametitle";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0) die("No records found");

// loop through the results returned by your query
while($data = mysql_fetch_assoc($res))
{
    $title=$data['gametitle'];
$cover=$data['cover'];
$gameid=$data['gameid'];

// directory for images
$dir="coverart";
?>



<div id="cardcontainer">

<div id="coverart">
<?php echo "<img src='$dir/{$cover}' width='100' height='140'><br>"; ?>
</div>
<div id="gametitle">
<a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a>
</div>
<div id="friendrating"></div>
<div id="globalrating"></div>

</div>
<?php
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244213
Share on other sites

The If statement checks to see if gameid variable exists in the url (the review?gameid= part) AND that the value is a number

if (isset($_GET['gameid']) && is_numeric($_GET['gameid'])) {

Then it's added into another variable, mainly for ease of use and then escaped for added security, mysql_real_escape_string escapes all bad and potentially malicious characters or code.

	$gameid = mysql_real_escape_string($_GET['gameid']));

We then build the query, pretty much the same as your query before, but instead of getting all data, we just pick the row in the DB that has the gameid of the value that we choose.

$sql = "SELECT * FROM Games WHERE gameid = $gameid";

We execute the query.

	$res = mysql_query($sql);

Put the data gathered into an array.

	$data = mysql_fetch_assoc($res);

 

// However you'd like to format the html to output

// I'm assuming that the file you have put here is called review.php

 

} else {

Link to comment
https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244217
Share on other sites

the code that conker posted will always return false, since he uses the is_int function on a $_GET value, query-strings that are passed to the $_GET array are strings, not integers..

 

<?php
//get results from db

if (!empty($_GET['gameid'])) { //if an id is allowed to be 0, replace with isset()
$gameid = $_GET['gameid']); // mysql_real_escape_string is only needed for db insertion
$sql = "SELECT * FROM Games WHERE gameid = $gameid";
$res = mysql_query($sql);
$data = mysql_fetch_array($res, MYSQL_ASSOC); //just the way I like to do it

// However you'd like to format the html to output

}else {

$sql = "SELECT * FROM Games ORDER BY gametitle";
$res = mysql_query($sql) or die(mysql_error()); //use mysql_error() for debugging only
if(mysql_num_rows($res) == 0){
      die("Mysql Error:".mysql_error());
}else{
      // loop through the results returned by your query
      while($data = mysql_fetch_assoc($res))
      {
          $title=$data['gametitle'];
  $cover=$data['cover'];
  $gameid=$data['gameid'];

  // directory for images
  $dir="coverart";
?>



<div id="cardcontainer">

<div id="coverart">
<?php echo "<img src='{$dir}/$cover' width='100' height='140'><br>"; ?>
</div>
<div id="gametitle">
<a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a>
</div>
<div id="friendrating"></div>
<div id="globalrating"></div>

</div>
<?php
}
}
?>

 

Edit: he changed it to is_numeric, which will work in this case

 

Link to comment
https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244218
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.