Undefined Index when retrieving data from POST

0

I'm creating an online store, and I need to add the products on the site through DB.

To facilitate, I want to create a graphical part, which is already done, but in my variables gives me the following error:

  

Notice: Undefined index: product_code in /home/i12184/public_html/admin.php on line 4
  Notice: Undefined index: product_name in /home/i12184/public_html/admin.php on line 5
  Notice: Undefined index: $ product_desc in /home/i12184/public_html/admin.php on line 6
  Notice: Undefined index: $ product_img_name in /home/i12184/public_html/admin.php on line 7
  Notice: Undefined index: price in /home/i12184/public_html/admin.php on line 8

My code:

$product_code = $_POST['product_code'];
$product_name = $_POST['product_name'];
$product_desc = $_POST['$product_desc'];
$product_img_name = $_POST['$product_img_name'];
$price =$_POST['price'];

$insert ='insert into products (product_code, product_name, product_desc, product_img_name, price)
   Values("'.$product_code.'", "'.$product_name.'", "'.$product_desc.'",
       "'.$product_img_name.'", "'.$price.'")';

mysql_query($insert);

Html

<form action="admin.php" method="post" onsubmit="formsubmit(this)">     
    Codigo do Produto: <input type="text" name="product_code" /><br /><br />
    Nome do Produto: <input type="text" name="product_name" /><br /><br />
    Descricao do Produto: <input type="tinytext" name="product_desc" /><br /><br />
    Nome da Imagem do Produto: <input type="text" name="product_img_name" /><br /><br />
    Preco: <input type="decimal" name="price" /><br /><br />
    <input type="submit" value="Enviar">
</form>

<script>    
    function formsubmit(form) 
    {       
        form.submit();  
    } 
</script>
    
asked by anonymous 27.03.2015 / 13:25

3 answers

1

Notice: "undefined index" :

To understand this type of message, look at the example below:

$x = $y + 10;

Running this script, what value should the variable $ x?

You must respond, impossible to say, I do not know what the value of the $ y variable is. For PHP the answer is the same, for it is doing the following operation internally:

$ x = undefined value + 10;

The correct way and do the following operation:

$y = 5;
$x = $y + 10;

Or

$y = 0;
$x = $y + 10;

Whenever an operation is performed with an "undefined" variable, PHP displays the notice: Undefined variable.

Often our programming problems are more complex because we are working with variables passed by SESSION, GET, POST and other methods, to work in this way and it is necessary to evaluate the condition of the variable, for example:

if(!isset($y))
$y = 0;

This script checks if the variable exists, if the variable does not exist, it is assigned zero.

The same error can occur when we are working with arrays and try to access a position that does not exist, for example:

$info_funcionario = array("nome" => "Patrick Espake", "profissao" => "Programador");
echo $info_funcionario["email"];

In the above script I am trying to access the "email" index in the array, this index does not exist and PHP displays the message Notice: Undefined index email.

You can make the following statement: "I have already programmed this in PHP and this error never appeared!".

What happens is that these error notifications can be configured in php.ini, many developers in the development environment leave configured to display all errors and in production environment leave configured not to display notification errors.

To configure php.ini to show all errors, you should leave the error_reporting clause as follows:

error_reporting  =  E_ALL

To configure php.ini to not display notification messages, you should leave it as follows:

error_reporting  =  E_ALL & ~E_NOTICE

In other programming languages an uninitialized variable can contain anything, in PHP it is considered null or 0 in an arithmetic operation.

In addition, it is faster to write:

echo $_GET['id'];

Than:

if isset($_GET['id'])) echo $_GET['id'];

In extreme cases you can use @ to hide notification messages.

echo @$_GET['id'];

Source: link

    
27.03.2015 / 14:02
1

All you have to do is data validation, because the POST array only receives the data from the fields that had data posted. The validation can be client-site, ie on the client side, which is the browser, for example:

<input type="text" name="product_code" required="required" />
<!-- Observe o required, não permitirá que seja postado em branco -->

if (isset($_POST['product_code']) === true) {
    $product_code = $_POST['product_code'];
} else {
    $product_code = false;
} 
    
31.03.2015 / 19:09
0
Just to complement and be a bit more straightforward, the point is that you're using apostrophes (% with%) and putting variables inside them so '...' , but variables only execute within '$minha_variavel' quotes, in the case of your example you do not need quotation marks, it would be enough to use "$minha_variavel"

The result should be something like:

$product_desc = $_POST[$product_desc];
$product_img_name = $_POST[$product_img_name];

But when I look at your code it actually seems to have another intent, as the code I've added will probably cause the warning [$minha_variavel] , looking at the html you have the following inputs: p>

<input type="text" name="product_code" /> 
<input type="text" name="product_name" /> 
<input type="tinytext" name="product_desc" /> 
<input type="text" name="product_img_name" /> 
<input type="decimal" name="price" /> 

None of them really have Undefined variable , so the correct one is to use this way:

$product_code = $_POST['product_code'];
$product_name = $_POST['product_name'];
$product_desc = $_POST['product_desc'];//Removido o $
$product_img_name = $_POST['product_img_name'];//Removido o $
$price =$_POST['price'];
    
31.03.2015 / 19:26