Web lists-archives.com

Re: [PHP] error Undefined index:




Hi.


Change FROM

$empresa = $_POST['empresa'];


TO

// To have a default value

$empresa =  ( isset( $_POST['empresa'] ) ) ? $_POST['empresa'] : '';


Alejandro Michelin Salomon


________________________________
De: Romero, Fernando <Fernando.Romero@xxxxxxxxxxxxxxxxxxxxxxx>
Enviado: quarta-feira, 28 de março de 2018 10:32
Para: Jigar Dhulla; php-general@xxxxxxxxxxxxx
Assunto: RE: [PHP] error Undefined index:



-----Mensaje original-----
De: Jigar Dhulla [mailto:jigar.tidus@xxxxxxxxx]
Enviado el: miércoles, 28 de marzo de 2018 8:33 a. m.
Para: php-general@xxxxxxxxxxxxx
Asunto: Re: [PHP] error Undefined index:

On Wed, Mar 28, 2018 at 4:11 PM, Romero, Fernando < Fernando.Romero@xxxxxxxxxxxxxxxxxxxxxxx> wrote:

>
>
>
>
> *De:* Jigar Dhulla [mailto:jigar.tidus@xxxxxxxxx] *Enviado el:*
> martes, 27 de marzo de 2018 3:38 p. m.
> *Para:* Romero, Fernando <Fernando.Romero@xxxxxxxxxxxxxxxxxxxxxxx>
> *CC:* php-general@xxxxxxxxxxxxx
> *Asunto:* Re: [PHP] error Undefined index:
>
>
>
>
>
>
>
> On Wed, Mar 28, 2018 at 12:04 AM, Romero, Fernando <Fernando.Romero@
> trenesargentinos.gob.ar> wrote:
>
> I have an error with a variable in $ _POST that I can not find how to
> solve it
>
> This is the error
>
> Notice: Undefined index: empresa in
> /var/www/jobs/ganancia_por_empresa_mes.php on line 34
>
> This is the query
>
> <?php
>
> $empresa = $_POST['empresa'];
> $mes = $_POST['meses'];
> $anio = $_POST['anio'];
>
>
>
> if ( $_POST ) {
> $result=pg_query("SELECT empresa, sum(valor) valor FROM trabajos WHERE
> empresa='$empresa' and to_char(fecha, 'TMMONTH')::varchar = '$mes' and
> date_part('year', fecha)= '$anio'
> group by empresa");
> $registros= pg_num_rows($result);
>
> }
> ?>
>
> <div align="center">
>   <table border="1" cellpadding="1">
>     <tr>
>       <td><strong>Empresa</strong></td>
>       <td><strong>Total</strong></td>
>
>     </tr>
>
> <?php
> //mostrar resultados
> for ($i=0;$i<$registros;$i++)
> {
>
> $row = pg_fetch_array ($result,$i );
>
> //}
> ?>
>       <tr>
>       <td>
>         <?php
>   echo $row['empresa'];
>   ?>
>       </td>
>       <td>$
>         <?php
>   echo $row['valor'];
>    }
>      ?>
>   </td>
>
>    </tr>
>
>   </table>
>
> </div>
>
>
> <div align="center" ><a
> href="ganancia_empresa_mes.php">Volver</a></div>
> </body>
>
> </html>
>
> The error of line 34 is this:
>
> $empresa = $_POST['empresa'];
>
> Thanks
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> http://www.php.net/unsub.php
>
> what is the output of ?
>
> var_dump( $_POST );
>
>
>
>
>
> --
>
>
> <https://about.me/jigar?promo=email_sig&utm_source=product&utm_medium=
> email_sig&utm_campaign=edit_panel&utm_content=thumb>
>
> *Jigar Dhulla*
>
> about.me/jigar
> <https://about.me/jigar?promo=email_sig&utm_source=product&utm_medium=
> email_sig&utm_campaign=edit_panel&utm_content=thumb>
>
> This is the file he calls
>
>
>
> <body class="Azul">
>
> <body style="background:#D3D3D3">
>
> <p>
>
> <body>
>
> <form action="ganancia_por_empresa_mes.php" method="post">
>
> <div align="center">
>
> <select name = "lispart" onchange="fun_lista();">
>
> <?php
>
> $SQL="SELECT nombre FROM empresas";
>
> $result = pg_query ($SQL ) or die("Error en la consulta SQL");
>
> $registros= pg_num_rows($result);
>
>
>
>   while ($row = pg_fetch_assoc($result))
>
> {
>
> echo "<option value='".$row['nombre']."'>".$row['nombre']."</option>";
>
> }
>
> ?>
>
>     </select>
>
>   <select name="meses" id="meses">
>
>     <option value="ENERO">enero</option>
>
>     <option value="FEBRERO">febrero</option>
>
>     <option value="MARZO">marzo</option>
>
>     <option value="ABRIL">abril</option>
>
>     <option value="MAYO">mayo</option>
>
>     <option value="JUNIO">junio</option>
>
>     <option value="JULIO">julio</option>
>
>     <option value="AGOSTO">agosto</option>
>
>     <option value="SEPTIEMBRE">septiembre</option>
>
>     <option value="OCTUBRE">octubre</option>
>
>     <option value="NOVIEMBRE">noviembre</option>
>
>     <option value="DICIEMBRE">diciembre</option>
>
>   </select>
>
>   <select name="anio" id="anio">
>
>     <option value="2018">2018</option>
>
>     <option value="2019">2019</option>
>
>     <option value="2020">2020</option>
>
>   </select>
>
>   <input type="submit" name="envio" id="envio" value="Enviar" />
>
> </div></td>
>
>     </tr>
>
>   </div>
>
> <label for="anio"></label>
>
> <div align="center"></div>
>
> </form>
>
> <div align="center"><a href="principal.php">Volver</a></div>
>
> </body>
>
> </html>
>

As Tim pointed out, there is no input element in your form which has attribute "name=empresa"


--
<https://about.me/jigar?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=edit_panel&utm_content=thumb>
Jigar Dhulla
about.me/jigar
<https://about.me/jigar?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=edit_panel&utm_content=thumb>

Thanks, solved