error insert data into php database

Asked

Viewed 417 times

-1

I am making an html page with the checkbox to insert in string form inside a table row, however if I do not mark all options it gives an error (but inserts in the database), how do I make this error disappear if in case I choose only a checkbox value?

Notice: Undefined index: plastico in C: xampp htdocs TCC postagem.php on line 4
Notice: Undefined index: papelao in C: xampp htdocs TCC postagem.php on line 5

include_once("setting.php");
$plastico = $_POST ['plastico'];
$papelao = $_POST ['papelao'];
$metal = $_POST['metal'];
$madeira = $_POST['madeira'];
$vidro = $_POST['vidro'];

$array = array ($plastico, $papelao, $metal, $madeira, $vidro);

foreach ($array as $key => $valor) {
    $campo[]= $valor;
    //echo $campo."<br/>";
}
$teste = implode(',',$campo);

 try{
  $sql = "INSERT INTO postagens (descricao) VALUES ('$teste')";
  echo $sql;
  $res = mysqli_query($conn, $sql);
  $linhas = mysqli_affected_rows($conn);

    if ($linhas ==1){
        echo "registro gravado com sucesso";
    }else{
        echo "falha na gravaçao";
    }
  • First of all it is not ERROR but a WARNING, what is occurring is that you are not sending in the POST the fields "plastic" and nor "cardboard".

  • yes, I expressed myself wrong, have to remove the message if I do not choose one of the checkbox options?

  • Put on top of your code: error_reporting (0);

  • Your variable has not been started, if you don’t want to give it importance, review the error_reporting of your hosting, or force your php.ini or . htaccess not to display this, you just need to know which mode runs the interpreter on the server to change the NOTICES, which are not errors. But you better start creating defined variables or treating them before using them to avoid NOTICES.

3 answers

1

Considering that vars when non-existent are for example 0, you can do the following: check if they exist and if not, set them to 0, this error occurs because Voce ta trying to catch a post that does not exist

if (isset($_POST['opcao'])) 
     $var = $_POST['opcao']; else
     $var = 0;

so, if it doesn’t have a value, it will be 0

0

That kind of warning Notice: Undefined index: is caused because you tried to call a $_POST variable that was not sent through the form.

With this scheme only those marked will return

HTML

put all the name of checkbox as material[]. These brackets treat elements of the same name as an array

<form id="myForm" action="" method="post">
    <input type="checkbox" name="material[]" value="plastico" />plastico<br>
    <input type="checkbox" name="material[]" value="papelao" />papelao<br>
    <input type="checkbox" name="material[]" value="metal" />metal<br>
    <input type="checkbox" name="material[]" value="madeira" />madeira<br>
     <input type="checkbox" name="material[]" value="vidro" />vidro<br>
    <input type="submit" value="Enviar">
</form>

PHP

if ((isset($_POST["material"])) && (!empty($_POST["material"]))){
    $opcoes = $_POST["material"];
    $materiais = implode( ",", $_POST['material'] );

    echo $materiais;

// ....
//..... "INSERT INTO postagens (descricao) VALUES ('$materiais')";


}

-1

Your error refers to the space between the $_POST and brackets for these 2 indexes, change

$plastico = $_POST ['plastico']; por $plastico = $_POST['plastico'];
$papelao = $_POST ['papelao']; por $papelao = $_POST['papelao'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.