K
It is very important that your code not only work, but that it be safe, so that the data is not exposed to the use of malicious code as the SQL Injection can be.The SQL Injection is a complex issue, but it brings some attention, as it concerns the security of our data. In the end I leave you some links.To avoid it, one of the best practices is the use of prepared consultationswhich consists of creating SQL instructions independent of the data sent. Thus, the SQL instruction is sent to the BD handler on the one hand (in this case using interrogation signs ? ) and on the other hand the data itself (in this case using bind_param->). There are other ways to do it. And you can also use PDO.Note that in the method bind_param the data stored in each variable is passed: $numero,$fechac,$fechav,$con,$sub,$iva,$total,$observaciones,$idus,$idp in the same order in which they appear INSERT INTO ... , you should also specify what type is each data that is happening, using the letters s or i. For more details about bind_param http://php.net/manual/es/mysqli-stmt.bind-param.php .A safer code, in your case, using prepared queries, would be more or less like this:/*
* Cadena de inserción sin pasar directamente los valores, sino usando ?
* IMPORTANTE: Debe haber tantos signos ? como nombres de columna
* Debe haber también la misma cantidad de valores en el bind_param
*/
$insertar ="
INSERT INTO pedido
(numero,fechaCreacion,fechaVencimiento,condiciones,subtotal,iva,total,observaciones,estado,Usuario_idUsuario,Proveedor_numeroIdent)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
/* Preparar la consulta */
$resultado = $conexion->prepare($insertar);
/* Puesto que prepare devuelve false si hay un error
podemos evaular tal condición directamente del modo siguiente
*/
if (!$resultado) {
echo "No se pudieron insertar los datos. Error: ".$conexion->error;
} else {
/*
MUY IMPORTANTE: Las letras issiiiisii indicarían el tipo de dato
de las columnas numero, fechaCreacion,fechaVencimiento,condiciones,subtotal,
iva,total,observaciones,estado,Usuario_idUsuario,Proveedor_numeroIdent
tal y como están declarados en la base de datos
en el ejemplo lo he puesto de forma intuitiva, debes verificarlo
y cambiar lo que haga falta
(ver enlace precedente por si hubiera otro tipo de datos distinto de s o i)
s indica un tipo de dato varchar, fecha, etc... i indica un tipo de dato numérico
TAMBIÉN IMPORTANTE: Debe haber tantos valores como los indicados más arriba
por los signos ? y cada columna debe coincidir
Lo que hace bind_param es pasar aparte los valores a insertar
a fin de evitar la Inyección SQL, como se ha explicado más arriba
*/
$resultado->bind_param('issiiiisii', $numero,$fechac,$fechav,$con,$sub,$iva,$total,$observaciones,$idus,$idp);
/* Ejecutar la inserción */
$resultado->execute();
/*
*Un una inserción hay que consultar affected_rows
*para saber si realmente se insertaron filas y cuántas
*/
$filasCreadas=$conexion->affected_rows;
echo "Se insertaron un total de $filasCreadas registros";
}
If the connection is okay, you should have no problems when inserting. Although I, in my case, would create a verification function using the unique indices to verify if the record exists before inserting it, but that is already another issue.I recommend that you dedicate a few minutes to understanding the SQL Injection problem. For this, the following can serve you: https://es.stackoverflow.com/q/18232/29967 http://php.net/manual/es/security.database.sql-injection.php