undefined index

0

Good evening, I have a code where I make an insert into, but this is returning me error, and I can not solve it, in the same field it is printed in the database, page and content. The page is registered because the content causes error; and the error  is: Notice: Undefined index: content my code:

       $pagina_editar = $_POST['pagina'];
       $content_editar = $_POST['content'];
       if(isset($_POST['cadastrar']) && $_POST['cadastrar'] == 'envia_form'){
$cadatrar_pagina = mysqli_query($conexao,"INSERT INTO lp_page (pagina, content) VALUES ('$pagina_editar', '$content_editar')")or die(mysqli_error($conexao));
       }

       if(isset($_POST['editar_post']) && $_POST['editar_post'] == 'envia_form'){
$atualiza_pagina = mysqli_query($conexao,"UPDATE lp_page SET content='$content_editar' WHERE pagina = '$pagina_editar'") or die (mysqli_error($conexao));
       }

      $pagina_de_edicao = $_POST['pagina'];

        $pega_pagina = mysqli_query($conexao,"SELECT id, pagina, content FROM lp_page WHERE pagina = '$pagina_de_edicao'")or die(mysqli_error($conexao));
           if(@mysqli_num_rows($pega_pagina) <= '0'){
     ?>

     <form action="" method="post" enctype="multipart/form-data" name="cadastrar_pagina">
     <input type="hidden" name="pagina" value="<?php echo $pagina_de_edicao;?>" />
     <textarea name="content" rows="30" cols=""></textarea>
     <input type="hidden" name="cadastrar" value="envia_form" />
     <input type="submit" value="Cadastrar" name="Cadastrar" class="cadastro_btn" />
     </form>    

     <?php

      }else{
        while($res_pagina = mysqli_fetch_array($pega_pagina)){
            $id = $res_pagina[0];
              $pagina = $res_pagina[1];
                $content = $res_pagina[2];  
   ?>
      <form action="" method="post" enctype="multipart/form-data" name="edita_pagina">
       <input type="hidden" name="pagina" value="<?php echo $pagina_de_edicao;?>"/>
      <textarea name="content" cols="" rows="30"> <?php echo $content; ?></textarea>
      <input type="hidden" name="editar_post" value="envia_form" />
      <input name="Editar" type="submit" value="Editar" class="cadastro_btn" />
      </form>
<?php
}
 }
 ?>

The error is occurring on the line: $ content_editar = $ _POST ['content'];  Can someone help me? Thank you

    
asked by anonymous 07.10.2015 / 03:41

1 answer

2

First, "Notice" is not an error.

php is only notifying you that the $ _POST array element, whose index is 'content', has not been defined.

It's easy to know why: See that you define the variable '$ content_edit' before to check if there is a POST request, that is, if the $ _POST array exists.

Solution: Define the variables after verifying that the POST type request exists.

if(
 isset($_POST['cadastrar']) && 
 $_POST['cadastrar'] == 'envia_form')
{
  $pagina_editar = $_POST['pagina'];
  $content_editar = $_POST['content'];
  $cadatrar_pagina = mysqli_query($conexao,"INSERT INTO lp_page (pagina, content) VALUES ('$pagina_editar', '$content_editar')")or die(mysqli_error($conexao));
}
    
07.10.2015 / 05:30