Variable value is lost when sending form

0

I EDIT WITH A POSSIBLE SOLUTION THAT I HAVE FOUND In the action of the form it was redifigating to 687/KILLALLZOMBIES/ with which it lost the value of the variable $id_comentario . I have left it empty and the problem has been solved. I do not know if it's the best way but it works well.

I am working with a variable (among others), which I receive through the URL. Specifically when entering the following URL 687/KILLALLZOMBIES/editar/211/ , the value of the variable $id_comentario would be 211 in this case. I pick it up with PHP using the following code:

$id_comentario = isset($_GET['id_comentario']) ? $_GET['id_comentario'] : "null";

I have verified that when entering the URL the variable is correct. My problem comes when I send a form, which is nothing more than a textarea to modify the comment. There is more code than what I put you but there you can see that the variable is declared outside the conditional if:

$id_comentario = isset($_GET['id_comentario']) ? $_GET['id_comentario'] : "null";

if ($conexion and isset($_POST['guardar_edicion'])) {

    $texto = para_vista_previa($_POST['comentario']);
    $texto = evitamos_script($texto);//EVITAMOS SCRIPTS

    $extractos = explode("[", $_POST['comentario']);


    foreach ($extractos as $extracto) {
        if ($pos = strpos($extracto, '/quote') !== false) {
            $total_quotes++;
        }
    }

    $errores = "";

    if (strlen($texto) > 1000) {
        $errores .= "El texto no puede contener más de 1.000 caracteres <br>";
    }

    if ($total_quotes > 3) {
        $errores .= "No puedes tener más de 3 citas en un mismo comentario <br>";
    }

    if (empty($errores)) {

        $quien_comenta = $user;

        $hoy = getdate();
        $meses = ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];

        $minuto = $hoy['minutes'];
        $hora = $hoy['hours'];
        $day = $hoy['mday'];
        $mes = $hoy['mon'];
        $segundos = $hoy['seconds'];

        if ($hoy['hours'] <= 9) {
            $hora = "0" . $hoy['hours'];
        }

        if ($hoy['minutes'] <= 9) {
            $minuto = "0" . $hoy['minutes'];
        }

        if ($hoy['mon'] <= 9) {
            $mes = "0" . $hoy['mon'];
        }

        if ($hoy['mday'] <= 9) {
            $day = "0" . $hoy['mday'];
        }

        if ($hoy['seconds'] <= 9) {
            $segundos = "0" . $hoy['seconds'];
        }

        $fecha_total_actual = $hoy['year'] . "-" . $mes . "-" . $day . " " . $hora . ":" . $minuto . ":" . $segundos;

        //comprobamos si hay menciones
        $resultado = array_unique(obten_menciones($texto));

        foreach ($resultado as $result) {

            if ($result != $_SESSION['usuario']) {
                $statement = $conexion->prepare("INSERT INTO menciones (de_quien, a_quien, en_que_hilo, foro, subforo, pagina_hilo, tipo, seccion) VALUES (:de_quien, :a_quien, :en_que_hilo, '-', '-', '-', 'mencion', 'noticia')");
                $statement->execute(array(":de_quien" => $_SESSION['usuario'],
                                        ":a_quien" => $result,
                                        ":en_que_hilo" => $en_que_ficha)
                                    );
            }

        }

        $statement = $conexion->prepare("UPDATE comentarios SET texto = :texto, editado_por = :editado_por, fecha_edicion = :fecha_edicion WHERE ID = :id");
        $statement->execute(array(":texto" => $texto,
                                ":editado_por" => $user,
                                ":fecha_edicion" => $fecha_total_actual,
                                ":id" => $id_comentario
                                ));

        //header("Location: " . $ruta . "Juegos/Xbox-One/$en_que_ficha/" . limpia_url($posts[0]['Juego']) . "/");


    }

The fact is that when sending the form the variable $id_comentario goes to "null", with which I can not update the database record. I have removed the redirect from the header (that is why it is commented), and curiously instead of redirecting 687/KILLALLZOMBIES/ , that is, as if I had some other redirect, which I have already looked at, and I only have another one that does not affect it. I usually find the solution but I just can not find it in this case.

    
asked by JetLagFox 06.06.2017 в 16:31
source

1 answer

0

The problem was the action of the form. I solved it with the following code:

<?php

$redirect = "";

if ($metodo == "editar") {
   $redirect = $ruta . "Juegos/Xbox-One/" . $en_que_ficha . "/" . limpia_url($posts[0]['Juego']) . "/editar/" . $id_comentario . "/" ;
} else {
   $redirect = $ruta . "Juegos/Xbox-One/" . $en_que_ficha . "/" . limpia_url($posts[0]['Juego']) . "/" ;
}

?>
<form class="form_comentarios_noticias" action="<?php echo $redirect ?>" method="post">
    
answered by 06.06.2017 в 17:36