0

I'm trying to update the database after select a few options and submit (using isset). The page display some information after a database query, and then I have to update the database according to the selected option. If I run exactly the same query that is inside the function "actualizarEstado" but on a third page, then it works. What am I doing wrong? I can't understand. Tried to kill and close the first connection, but I get the same results. Thanks in advice!

<?php

include '00-conexion.php';

$data = extract($_GET);

$sql = "SELECT * FROM inscripciones WHERE nroInscripcion = $sel";

$retval = mysqli_query($conexion, $sql);
$fila = mysqli_fetch_array($retval);


if(isset($_POST['ejecutar'])){
    actualizarEstado($sel);
}

function actualizarEstado($sel){
    $estado =  $_POST['estado'];

    $sql2 = "UPDATE inscripciones SET revision1='',
        revision2='',
        revision3='',
        revision4='',
        revision5='',
        revision6='',
        revision7='',
        revision8='',
        revision9='',
        estado='$estado'
        WHERE nroInscripcion = $sel";

    if (!mysqli_query($conexion, $sql2)) {
        die('Error: ' . mysqli_error($conexion));
    }
}

?>

<form method="post" action="">
        <tr><td><select name="estado" from="estado">
            <option value="aceptado">Aceptar</option>
            <option value="rechazado">Rechazar</option>
            <option value="revision">En revision</option>
        </tr/></td>
        <tr><td><input type="submit" value="Actualizar" name="ejecutar" onclick="return confirm('¿Estás seguro que deseas?')" /></tr></td>
</form>
DaNoiseMan
  • 139
  • 2
  • 13
  • Does It throws any error? – Jacobson Nov 25 '15 at 00:53
  • No, at all. That's the most annoying thing. BTW, I'm running it locally, PHP Core Version 5.4.44-0+deb7u1, it should have to be ok. I'm updating from mysql to mysqli. – DaNoiseMan Nov 25 '15 at 00:56
  • It seems your $conexion is out of context. Try to load it from globals, or pass it as a function parameter. – Jacobson Nov 25 '15 at 01:01
  • I had a similar issue when migrating from mysql to mysqli api. Although my PHP says an error occurred, mysql doesn't returned any message. So you have to turn on this mysqli error reporting. Take a look at this [link](http://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli/21048373#21048373). – Jacobson Nov 25 '15 at 01:09
  • Tried everything, even turning on mysqli error reporting. Once I set $sql2, then it doesn't echo anything, but exactly the same instruction on another page, works great. It's freaking me out. – DaNoiseMan Nov 25 '15 at 17:15

1 Answers1

0

I think you have to pass the $connexion variable through the function!

Make it something like this:

function actualizarEstado($myConnection,$sel){
    $estado =  $_POST['estado'];

    $sql2 = "UPDATE inscripciones SET revision1='',
    revision2='',
    revision3='',
    revision4='',
    revision5='',
    revision6='',
    revision7='',
    revision8='',
    revision9='',
    estado='$estado'
    WHERE nroInscripcion = $sel";

    if (!mysqli_query($myConnection, $sql2)) {
        die('Error: ' . mysqli_error($myConnection));
    }
}

and call the function like: actualizarEstado($connexion,$sel);

  • Other than that, you can make the connection inside the function itself. – mankybansal Nov 25 '15 at 00:57
  • I tried both, none of them worked. It's really strange, once I set $sql2, then it doesn't echo anything, but exactly the same instruction on another page, works great. – DaNoiseMan Nov 25 '15 at 17:13
  • You must be forgetting some variable which is out of scope. Do go through the code on that other page of yours on which it's working, compare and see if you've declared all the variables and this function has access to them! – mankybansal Nov 25 '15 at 17:37