Neither of your two queries meets the criteria of prepared queries .
In summary, a prepared consultation comprises several stages:
Write the query to prepare, which should not carry data in itself, but markers for each data.
Send to prepare the query with prepare
Pass the values separately using the bind_param
method
Run the query with execute
Eventually make a bind_value
to get the values (in case of queries SELECT
). Or use another method of storing results.
To meet these criteria, you can write your code like this:
$sqlCliente="SELECT id_cliente FROM clientes WHERE correo=?";
$stmtCliente=mysqli_prepare($conexion,$sqlCliente);
/*
La letra "s" indica que el tipo de dato es varchar (string)
si fuese un entero se pondría una "i"
si fuese un doble una "d" y si fuese del tipo blob una "b"
*/
mysqli_stmt_bind_param($stmtCliente,"s",$correo);
mysqli_stmt_execute($stmtCliente);
mysqli_stmt_bind_result($stmtCliente, $idCliente);
$sqlCodigo="INSERT INTO codigos (codigo,fecha_antigua,id_cliente) VALUES (?,?,?)";
$stmtCodigo=mysqli_prepare($conexion,$sqlCodigo);
mysqli_stmt_bind_param($stmtCodigo,"ssi",$codigo,$fecha,$idCliente);
mysqli_stmt_execute($stmtCodigo);
I have used more descriptive variable names. And I have respected the procedural style that you show in your code, although the object-oriented style is more modern and should be learned.
If there is any doubt you can say it in comments.
I hope you find it useful.