How to avoid SQL injection in this Php code and create MYSQL queues? [duplicate]

2

I have this code in a system that I am developing, it works but I know that I have to put security in it and also leave it to work in the long term, for which I would like to know how to prevent the database from falling down, to several users enter information at the same time, finally there is a problem if I do not divide the entire code into classes?

<?php
if(isset($_POST["submit"])){
include_once 'resource/Database.php';


try {


$sql = "INSERT INTO capturar_pedido (cliente,
orden_de_compra,
producto,
unidad,
cantidad,
fecha_de_embarque,
notas,
etiquetado)
VALUES ('".$_POST["cliente"]."','".$_POST["orden_de_compra"]."','".$_POST["producto"]."','".$_POST["unidad"]."','".$_POST["cantidad"]."','".$_POST["fecha_de_embarque"]."','".$_POST["notas"]."','".$_POST["etiquetado"]."')";


if ($db->query($sql)) {
     echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
} 
else{
     echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
}

    $db = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

}

?>

<?php
include_once 'resource/Database.php';

$sql = $db->prepare("SELECT * FROM capturar_pedido");
$sql->setFetchMode(PDO::FETCH_ASSOC);
$sql->execute();


if($sql->rowCount() != 0) {

?>
<table class="table table-condensed">
   <tr>
      <td>cliente</td>
      <td>Orden de compra</td>
      <td>Producto</td>
             <td>Unidad</td>
             <td>Cantidad</td>
      <td>Fecha de embarque</td>
      <td>Notas</td>
      <td>Etiquetado</td>


   </tr>
 <?php     
 while($row=$sql->fetch()) 
 {
      echo "<tr>".
           "<td>".$row["cliente"]."</td>".
           "<td>".$row["orden_de_compra"]."</td>".
           "<td>".$row["producto"]."</td>".
                     "<td>".$row["unidad"]."</td>".
           "<td>".$row["cantidad"]."</td>".
                     "<td>".$row["fecha_de_embarque"]."</td>".
           "<td>".$row["notas"]."</td>".
           "<td>".$row["etiquetado"]."</td>".


           "</tr>";
 }

}
else
{
     echo "don't exist records for list on the table";
}

?>
</table>
    
asked by Daniel Treviño 11.08.2017 в 01:20
source

1 answer

2

To avoid SQL injection, use prepared statements to access the database.
When you do this in the query string you use question marks to mark where the data will go next.
The data itself is associated with the query in a second instance that allows the database to know positively that even if you get sql injected, it is still data.

    
answered by 11.08.2017 в 01:49