0

We are currently working on a game shop website and have hit a roadblock regarding the purchase link. The link displays within a mysql table and each link sends the user to the same page. This is necessary as we will be adding new games to the database and want to do using only a mysql command to make the site as efficient as possible.

This is the code of the table (ignore the fact that the purchase link displays the 'gameCodes'.

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['gameName'] . "</td>";
  echo "<td>" . $row['pointsValue'] . "</td>";
  echo "<td>" . '<a href="Purchase.php">'. $row['gameCodes'] .'</a>' .  "</td>";
  echo "</tr>";
  }
echo "</table>";

What I am wanting to do is send the game code of the game that corresponds with the row the link is on to the Purchase.php page to then process the purchase.

Any help is appreciated greatly.

Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
  • You can put the game code in a variable in the link, but I would make sure you do quite a bit of SQL injection protection especially when dealing with products and purchasing – Chitowns24 Mar 23 '14 at 17:20

3 Answers3

1

I think you can put the gameCodes id directly in the link

echo "<td>" . 
        '<a href="Purchase.php?gameCodes='. $row['gameCodes'] .'">'.
            $row['gameCodes'] . 
        '</a>' .
    "</td>";

Now you can process the code from the purchase page and retrieve it with $_GET

$_GET['gameCodes'];
Patrick Maciel
  • 4,874
  • 8
  • 40
  • 80
Fabio
  • 23,183
  • 12
  • 55
  • 64
1

Have you thought about sending it through the URL like follow:

while($row = mysqli_fetch_array($result))
{
 echo "<tr>";
 echo "<td>" . $row['gameName'] . "</td>";
 echo "<td>" . $row['pointsValue'] . "</td>";
 echo "<td>" . '<a href="Purchase.php?'.$row['gameCodes'].'">'. $row['gameCodes'] .'</a>' .  "</td>";
 echo "</tr>";
 }
echo "</table>";

and then process to the purchase using the code sent in the URL let me know if it corresponds to what you need.

Hosni
  • 668
  • 8
  • 29
1

my answer deals with not only passing variables from url to your page....but passing it in clean way

echo "<td>" . 
        '<a href="Purchase.php?gameCodes='.urlencode($row['gameCodes']) .'">'.
            $row['gameCodes'] . 
        '</a>' .
    "</td>";
  • Then to fetch the data strip_tags from the url variable if any:

    echo (strip_tags($_GET['item']));

why is this needed??

Since you are fetching the values from URL, assume i manually change the url to :

Purchase.php?gameCodes=<script>alert("hello")</script>

then without proper handling, gameCodes variable value will be fetched and it would alert "hello" on the page

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
  • @ChrisPBacon : welcome...i'll suggest if u look into htmlentities too....they are helpful if u are showing user given content on your page!!! http://in3.php.net/htmlentities | and | http://stackoverflow.com/questions/46483/htmlentities-vs-htmlspecialchars – NoobEditor Mar 23 '14 at 17:48