Query in MySQL from PHP

0

I am adding a record in MySQL from PHP , but I get the following error message.

  

"Notice: Undefined index: id in ..." "Notice: Undefined index: name   in ... "" Notice: Undefined index: sex in ... "

This is the code snippet.

$identificacion=$_POST["id"];
$nombre=$_POST["nombre"];
$sexo=$_POST["sexo"];
$password=123;
$telefono=$_POST["telefono"];
$correo=$_POST["correo"];
$rol=2;
$fecha=$_POST["anio"].$_POST["mes"].$_POST["dia"];

function agregado(){
  $query="INSERT INTO paciente VALUES('$identificacion','$nombre','$sexo','$password',
 '$telefono','$correo','$rol','$fecha')";
  $agregarPaciente = mysqli_query($conexion,$query);
} 
    
asked by Niko Wilches 07.05.2017 в 03:31
source

2 answers

1

With this, but using it with all the other variables, you can ensure that it exists and is not empty, so we are discarding options until we reach the solution.

if (isset($_POST["sexo"]) && isset($_POST["nombre"])  && !empty($_POST["sexo"]) && !empty($_POST["nombre"]) ){

$nombre=$_POST["nombre"];
$sexo=$_POST["sexo"];

}

Now it would be great if you could pass the data as arguments of the function something like this, it has happened to me that the functions do not recognize the variables that are not inherent to itself only if they are decalitated as global or public, my solution to pass them to the function as arguments.

    if (isset($_POST["sexo"]) && isset($_POST["nombre"]) && isset($_POST["identificacion"]) && isset($_POST["telefono"]) && isset($_POST["password"]) && isset($_POST["correo"]) && isset($_POST["rol"]) && isset($_POST["fecha"]) && !empty($_POST["sexo"]) && !empty($_POST["nombre"]) && !empty($_POST["identificacion"]) && !empty($_POST["telefono"]) && !empty($_POST["password"]) && !empty($_POST["correo"]) && !empty($_POST["rol"]) && !empty($_POST["fecha"]) )
{

    $identificacion=$_POST["id"];
    $nombre=$_POST["nombre"];
    $sexo=$_POST["sexo"];
    $password=123;
    $telefono=$_POST["telefono"];
    $correo=$_POST["correo"];
    $rol=2;
    $fecha=$_POST["anio"].$_POST["mes"].$_POST["dia"];
    agregado($identificacion,$nombre,$sexo,password,$telefono,$correo,$rol,$fecha);
    }
    function agregado($identificacion,$nombre,$sexo,password,$telefono,$correo,$rol,$fecha ){
      $query="INSERT INTO paciente VALUES('$identificacion','$nombre','$sexo','$password',
     '$telefono','$correo','$rol','$fecha')";
      $agregarPaciente = mysqli_query($conexion,$query);
    } 
    
answered by 07.05.2017 в 03:46
1

If your form is formed in this way:

<form action="agregarPacienteBD.php" id="formAgregar" method="POST">
    <input type="text" id="nombre"  size="30" maxlength="255" value="" />
    <input type="text" id="id" size="30" maxlength="255" value="" />
    <input type="text" id="telefono" size="30" maxlength="255" value="" />
    <input type="text" id="correo" size="30" maxlength="255" value="" />
</form>

When trying to collect the values sent to the form with $_POST["nombre"] , $_POST["id"] , etc ... it will not work for you, because the attribute that $ _POST uses to index the data is the attribute name of the <input> , not in the attribute id . Therefore:

<form action="agregarPacienteBD.php" id="formAgregar" method="POST">
    <input type="text" name="nombre"  size="30" maxlength="255" value="" />
    <input type="text" name="id" size="30" maxlength="255" value="" />
    <input type="text" name="telefono" size="30" maxlength="255" value="" />
    <input type="text" name="correo" size="30" maxlength="255" value="" />
</form>

It must be said that the PHP Manual documentation does not say it clearly, unfortunately . Appears in comment, by one of the contributors:

  

Make sure your input items have the NAME attribute. The id attribute   is not enough! The name attribute on your input controls is what   $ _POST uses to index the data and therefore show the results.

  Make sure that the input elements have the NAME attribute. The   id attribute is not enough! The name attribute in the controls   input is what $ _POST uses to index the data and, therefore,   So, show the results.
    
answered by 07.05.2017 в 17:23