Bring SQL statement and move to PHP variable

0

I have this SQL / PHP statement

echo '¡Hola ' . htmlspecialchars($_POST["div"]) . '!';

$sql = "SELECT id_div FROM divisiones WHERE division_name = '$_POST[div]'";
$res = mysqli_query($conexion, $sql);

And what I want is to pass the id_div that brings the sentence to a php variable to be able to insert it into an insert that I will do with SQL.

But it does not work and I think it's because I have this part wrong that I put.

How could I do it?

EDIT:

I add the code I use to insert it into an INSERT statement, I do not know if it's okay.

        mysqli_query($conexion, "insert into result_equipo (id_reseq, id_div, jor, id_eq, puntos, pg, pp, n_p, fecha) VALUES 
    (NULL, '$res', '$_POST[jor]', '$_POST[equipos]', '2', '1', '0', '0', '$_POST[date]')");

Thanks

    
asked by Javi Tercero 20.09.2018 в 14:37
source

2 answers

2

You are missing a lot of meat in your code, I would recommend a PHP YouTube course.

Now, do not break your head, I leave the code commented.

Greetings.

<?php

//Primero tienes que validar que llegue un dato, lo hice con una condición ternaria
$div = !empty( $_POST['div'] ) ? $_POST['div'] : null;

//Creas tu consulta
$sql = "SELECT id_div FROM DIVISIONES WHERE division_name = '" . $div . "'";

//Ejecutas la consulta, voy a suponer que tu variable $conexion no tiene problemas
$res = mysqli_query( $conexion, $sql );

//Valida que no haya problemas con la consulta, mysqli_query te regresa un vector de datos o false
if( !$res ){
  echo 'La consulta está mal construida';
}

else{
  //Si la consulta está bien ahora revisa que existan datos
  if( mysqli_num_rows($res) <= 0){
    echo 'No existe información con esta variable';
  }

  else{
    //Si trae datos la consulta entonces ya puedes usarla, hay que sacar los datos
    //Como solo es un dato podemos hacerlo directo, si fueran más, entonces hacemos un while
    $row = mysqli_fetch_array( $res );

    //Saca la variable, así como la llamaste en tu SQL vendrá el índice en tu resultado
    //Supondré que es un INT o similar, por lo que parsearé, si no es así solo quita (int)
    $in_div = (int) $row['id_div'];

    //Recuerda que por salud del servidor hay que liberar memoria
    mysqli_free_result($res);

    //Ahora si ya puedes usar el id en tu Insert;
    $insert = "insert into result_equipo (id_reseq, id_div, jor, id_eq, puntos, pg, pp, n_p, fecha) VALUES 
    (NULL, '$in_div', '$_POST[jor]', '$_POST[equipos]', '2', '1', '0', '0', '$_POST[date]')";

    //Ejecuta tu INSERT
    mysqli_query($conexion, $insert);

    //Vamos validar la inserción
    if( mysqli_affected_rows($conexion)<=0 ){
      echo 'No se insertó nada';
    }

    else{
      //En caso que necesites el ID que se insertó lo puedes hacer así
      $last_id = mysqli_insert_id($conexion);
      echo 'Inserción correcta, el ID es: ' . $last_id;
    }

    //Recuerda cerrar la conexión
    mysqli_close( $conexion );
  }
}

?>
    
answered by 20.09.2018 / 15:25
source
1

You can not put an array like this directly in your statement SQL the correct form would be between keys {} or concatenated with . asi

$sql = "SELECT id_div FROM divisiones WHERE division_name = '{$_POST[div]}'";


  mysqli_query($conexion, "insert into result_equipo (id_reseq, id_div, jor, id_eq, puntos, pg, pp, n_p, fecha) VALUES 
    (NULL, '$res', '{$_POST[jor]}', '{$_POST[equipos]}', '2', '1', '0', '0', '{$_POST[date]}')");

or so

 $sql = "SELECT id_div FROM divisiones WHERE division_name = '{$_POST[div]}'";


      mysqli_query($conexion, "insert into result_equipo (id_reseq, id_div, jor, id_eq, puntos, pg, pp, n_p, fecha) VALUES 
        (NULL, '$res', '".$_POST[jor]."', '".$_POST[equipos]."', '2', '1', '0', '0', '".$_POST[date]."')");
    
answered by 20.09.2018 в 15:33