0

Hello all I am doing a form in php since drupal in which I have a text box and a push button, the input of a code (varchar) and this is compared to a record in sqlserver at the time of performing the validation Code entered In the form exists in the DB) executes an iframe with a stored procedure.

This is the code, I think I'm doing something wrong with the php code and select it because the moment of entering the value to query does not generate the text "all right" or the "number consulted does not exist" found in He does

I thank those who tell me to help and guide

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Buscador php</title>
<form method="POST" action="" onSubmit="return validarForm(this)">

    <input type="text" placeholder="Buscar usuario" name="palabra">

    <input type="submit" value="Buscar" name="buscar">

</form>
</meta>


 <script type="text/javascript">
    function validarForm(formulario) 
    {
        if(formulario.palabra.value.length==0) 
        { //¿Tiene 0 caracteres?
            formulario.palabra.focus();  // Damos el foco al control
            alert('Debes rellenar este campo'); //Mostramos el mensaje
            return false; 
         } //devolvemos el foco  
         return true; //Si ha llegado hasta aquí, es que todo es correcto 
     }   
</script>
    </html>
</head>
<?php
//si existe una petición  
if($_POST['buscar']) 
{   

$usuario= '';
$pass = '';
$servidor = ''; 
$basedatos = '';
$info = array('Database'=>$basedatos, 'UID'=>$usuario, 'PWD'=>$pass); 
$conexion = sqlsrv_connect($servidor, $info);  
if(!$conexion){

 die( print_r( sqlsrv_errors(), true));

 }

echo 'Conectado';


$buscar = $_POST["palabra"];

$arrojado = sqlsrv_query("SELECT ID from Venta 
where ID like '%buscar%'",$conexion) or 
die("Problemas en el select:".sqlsrv_error()); 
?>
//$resultado = sqlsrv_fetch_array($arrojado); 

<?php
$valor = $resultado['aleatoria']; //LE ASIGNAMOS UNA VARIABLE AL RESULTADO DE LA CONSULTA 

//COMPARAMOS CON UNA CONDICIONAL 

if($arrojado == $buscar){ 
echo"todo bien";

} 
else { 
echo "El numero consultado no existe"; 
?>
miken32
  • 42,008
  • 16
  • 111
  • 154

1 Answers1

1

First you should read the answers to this question: How to get useful error messages in PHP? It would have saved you a lot of trouble.

You are trying to compare a database result to a string, which will not work:

$buscar = $_POST["palabra"];
...
$arrojado = sqlsrv_query("...");
...
if($arrojado == $buscar) {
...
}

You're also passing parameters to sqlsrv_query() in the wrong order, and were missing 2 closing braces at the end of your code and a space after an echo statement.

You need to fetch the row from the result set, and then get the first entry from it:

<?php
if($_POST['buscar']) {

    $usuario= '';
    $pass = '';
    $servidor = ''; 
    $basedatos = '';
    $info = array('Database'=>$basedatos, 'UID'=>$usuario, 'PWD'=>$pass); 
    $conexion = sqlsrv_connect($servidor, $info);  
    if(!$conexion){
        die( print_r( sqlsrv_errors(), true));
    }
    echo 'Conectado';

    $buscar = $_POST["palabra"];

    $arrojado = sqlsrv_query($conexion, "SELECT `ID` FROM Venta WHERE `ID` LIKE '%buscar%'");
    if (!$arrojado) {
        die("Problemas en el select:".sqlsrv_error());
    }
    $row = sqlsrv_fetch_array($arrojado, SQLSRV_FETCH_ASSOC);

    //COMPARAMOS CON UNA CONDICIONAL 
    if($arrojado["ID"] == $buscar){
        echo "todo bien";
    } else { 
        echo "El numero consultado no existe";
    }
}
?>
miken32
  • 42,008
  • 16
  • 111
  • 154