-3

I have a code on my website that calls upon the pin of a specific row and than echos data from that row. I need to know how i can prevent an SQL injection using the code.

Here it is:

<?php
if (isset($_GET['pin']))
{
$con=mysqli_connect("server.com","username","password","database");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT * FROM beta WHERE pin = " . $_GET['pin']);
while($row = mysqli_fetch_array($result))
  {
  echo "<table width='600px'><td width='200px'><font face='arial' size='2px'>" . $row['name_pin'] . " " . $row['pin'] . "</font></td><td width='200px'><font face='arial' size='2px'>" . $row['name_info'] . "" . $row['info'] . "</font></td><td width='200px'><font face='arial' size='2px'>" . $row['name_stat'] . " " . $row['stat'] . "</font></td></table>";
  echo "<br>";
  }
mysqli_close($con);
}
?>

Thanks for all the help in advance! P.S.: The more informational you are the better.

John Woo
  • 258,903
  • 69
  • 498
  • 492
TheCod3r
  • 1
  • 5

2 Answers2

4

parameterized the value,

$pin = $_GET['pin'];
$stmt = $dbConnection->prepare('SELECT * FROM beta WHERE pin =  ?');
$stmt->bind_param('s', $pin);
$stmt->execute();
John Woo
  • 258,903
  • 69
  • 498
  • 492
0
<?php
if (isset($_GET['pin'])){
$con=mysqli_connect("server.com","username","password","database");
$pin= mysqli_real_escape_string($con, $_GET['pin']);
//rest of code, 

http://php.net/manual/en/mysqli.real-escape-string.php

web2students.com
  • 307
  • 2
  • 16