I would like to carry out a validation where, before uploading the XML file, it is verified in a database table if the field clave
is different from what is obtained from the XML reading, if it is different from what the XML reading contains, allow it to upload otherwise display a message indicating that the clave
.
Initially I clave
am already obtaining it from reading an XML file as follows:
if($res['xml']) {
if( $_FILES['XmlToUpload']['error'] !== UPLOAD_ERR_OK ){
// error_log($errores[$_FILES['XmlToUpload']['error']]);
die( $errores[$_FILES['XmlToUpload']['error']] );
}
$xml = new SimpleXMlElement( $_FILES['XmlToUpload']['tmp_name'], 0, true );
$clave = (string)$xml->children('cfdi',true)->Complemento->children('tfd',true)->attributes()['CLAVE'];
include './db/conectar.php';
if( $conn === false ){
die(print_r(sqlsrv_errors(), true));
}
$sql = "{call ValidarXML(?,?,?)}";
$params = array($rfc, $total, $factura,);
$result = sqlsrv_query( $conn, $sql, $params );
if( $row = sqlsrv_fetch_array($result) ){
$archivoXML = $_FILES['XmlToUpload']['name'];
$rutaXML = "/XML/";
$filepathXML = $rutaXML.$archivoXML;
if(move_uploaded_file( $_FILES['XmlToUpload']['tmp_name'], $rutaXML . $archivoXML)){
echo 'Documentos cargados exitosamente';
}
else {
echo 'Error al mover a directorio destino';
}
} else {
echo 'Por favor verifique el contenido de los documentos';
}
}
The query with which I intend to validate in the database is the following:
SELECT CLAVE FROM PURCHASEORDER WHERE CLAVE = ?;
I hope someone can give me some guidance on this validation.
Update 1:
I have created the following stored procedure based on the comments to check if it exists or not:
ALTER PROCEDURE [dbo].[SPValidarClave]
-- Add the parameters for the stored procedure here
@CLAVE VARCHAR(100)
AS
BEGIN
SET NOCOUNT ON;
/**/
Declare @Existe int
if not exists(Select 1 From PURCHASEORDER WHERE CLAVE=@CLAVE)
begin
set @Existe =1
end
else
begin
set @Existe =0
end
Select @Existe
/**/
END
With this stored procedure done, I only require validation from my PHP code.
You can create a validation function that receives the key as a parameter, checks if there are records with that key and returns
true
orfalse
depending on the case.All of this can be done with a simple query, without the need to use stored procedures.
For example:
Now to call it:
Post-Data : If you want to do the same thing from an SP, you only have to change
$sql
to a call to the SP.To adapt it to the SP that you have indicated in your question, it would be more or less like this.
I have put comments in the code explaining what's new. If you don't understand something, ask in comments.