Localhost says 'undefined'

3

This is my back end, which seeks to update the user's nickname

<?php

session_start (); 

include_once 'conexion.php';


$respuesta = array();



if( empty($_REQUEST['nick-nuevo']))
{
$respuesta['mensaje'] = 'Nuevo nick, vacio';
}


else

{
$nicknuevo = $_REQUEST['nick-nuevo'];

$sql = 'UPDATE Usuario SET nick = "$nicknuevo" WHERE id = "$_SESSION["id"]"';
mysqli_query ($conn, $sql); 


}

Investigating in StackOverflow, I came across this This however I do not think it has any variable undefined, try doing it with this

$sql = "UPDATE Usuario SET nick = '". $nicknuevo. "' WHERE id = '".$_SESSION['id']"'";

To see if it changed something and it tells me the following error

  

[Parse error: syntax error, unexpected '"'" '(T_CONSTANT_ENCAPSED_STRING)] that according to this I have my bad quotes link but from my perspective everything seems to be in order, I do not see any error.

ANNEX

<?php 

include_once 'header.php'
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
        $(function() {
            $(document).on('click', '#update-btn', function (e) {
                e.preventDefault();

                var obj = {  
                    'nick-nuevo': $('#nick-nuevo').val().trim(),  
                };


                $.ajax({
                    url: 'back_update.php',
                    method: 'POST',
                    data: obj,
                    success: function ( respuesta ){
                        alert( respuesta.mensaje );
                    },
                    error: function( e, err, error ){
                    //Añadimos un nodo de error, por si pasa algo en el servidor, esto lo vamos a ver en la consola de depuración
                     console.log(e, err, error);
                    }
                })
            });
        })
</script>


        <div class="main-wrapper"> 

            <form id="update-form" class="signup-form" method="POST"> 
            <h2>Actualizar</h2>


                <input id="nick-nuevo"type="text" name="nick_nuevo" placeholder="nick nuevo" required> </input>


            <button id="update-btn"type="submit" name="submit" value="actualizar"> 
            Actualizar
            </button>


            </form>
<?php 

include_once'footer.php';
?>
    
asked by Hoozuki 24.08.2018 в 16:20
source

2 answers

4

I think you're already my Padawan. You have a few errors in your code and you can refine a bit, I'll explain:

<?php

session_start (); 
include_once 'conexion.php';
$respuesta = array();

if( empty($_REQUEST['nick-nuevo'])){
$respuesta['mensaje'] = 'Nuevo nick, vacio';
}


else{
  $nicknuevo = $_REQUEST['nick-nuevo'];

  //Tu tienes esto
  $sql = "UPDATE Usuario SET nick = '". $nicknuevo. "' WHERE id = '".$_SESSION['id']"'";

  //Se te olvido concatenar la última parte con un " . ", así debería quedar
  $sql = "UPDATE Usuario SET nick = '". $nicknuevo. "' WHERE id = '".$_SESSION['id'] . "'";
  mysqli_query($conn, $sql);    

  //Puedes comprobar la actualización del campo así
  if( mysqli_affected_rows($conn) > 0){
    $respuesta['mensaje'] = 'Se actualizó correctamente el registro';
  }

  else{
    $respuesta['mensaje'] = 'No fue posible actualizar el registro';
  }
}

//Se te está pasando regresar $respuesta en formato JSON y su encabezado
header('Content-Type: application/json');
echo json_encode( $respuesta );
?>

That's how it should work for you

Greetings

    
answered by 24.08.2018 / 16:38
source
1

Try this

    <?php

session_start (); 

include_once 'conexion.php';

$respuesta = array();

if( empty($_REQUEST['nick-nuevo']))
{
    $respuesta['mensaje'] = 'Nuevo nick, vacio';

}else{

    $nicknuevo = $_REQUEST['nick-nuevo'];
    $id = $_SESSION["id"];

    $sql = "UPDATE Usuario SET nick = '".$nicknuevo."' WHERE id = ".$id;

    $result = mysqli_query($conn, $sql);

     if(!$result)
     { 
        $respuesta['mensaje'] = 'Error al actualizar';

     } else{ 

        $respuesta['mensaje'] = 'Actualizado con exito';
    } 

}

?>
    
answered by 24.08.2018 в 16:34