Undefined Index while recovering data from POST

Asked

Viewed 6,109 times

2

I’m creating an online store, and I need to add the products on the site through the comic book.

To make it easier, 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>
  • 1

    For what you’re saying in your mistake $_POST['product_code'] does not exist. Could post the result of var_dump($_POST) ?

  • @Erloncharles at the moment I can’t do it :/

  • 2

    Could put part of the html form?

  • Could complement and add this your javascript method formsubmit() ? Just to make it right: $_POST['$product_desc']; note that you are using the $ in front of the name. And here too: $_POST['$product_img_name'];. Take this $ and stay like this: $_POST['product_img_name']; $_POST['product_desc'];

  • 1

    @Rafaelwithoeft done :) really hadn’t noticed the '$' thank you so much

  • Was resolved:?

  • Ja funciona Thanks to everyone simply move the php code to another file.

Show 2 more comments

3 answers

3

Notice: "Undefined index":

To understand this type of message, note the example below:

$x = $y + 10;

Running this script, which value should contain the $x variable?

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

$x = undefined value + 10;

The right way and do the following operation:

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

Or

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

Every time an operation is performed with an "undefined value" variable PHP displays the Notice: Undefined variable message.

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 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 is assigned zero.

The same error may 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 index "email" in the array, this index does not exist and PHP displays the message Notice: Undefined index email.

You can make the following statement: "I’ve programmed like this in PHP and never appeared this error!".

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

To set up php.ini to show all errors, you must leave the error_reporting clause as follows:

error_reporting  =  E_ALL

To set up php.ini to not display the 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.

Besides, it’s quicker 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:http://www.htmlstaff.org/ver.php?id=11074

2

All you need to do is validate the data, because the POST array only receives data from the fields that had data posted. Validation can be client-site, that is, 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 -->

And/or can be validated on the server-side, ie in PHP, example:

if (isset($_POST['product_code']) === true) {
    $product_code = $_POST['product_code'];
} else {
    $product_code = false;
} 

1

Just to complement and be a little more direct, the point is that you are using apostrophes ('...') and putting variables inside them like this '$minha_variavel', but variables only perform inside quotation marks "$minha_variavel", in the case of your example you don’t need quotes, just use so [$minha_variavel]

The result should be something like:

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

But when analyzing your code it actually seems to have another intention, because probably the code I added will cause the Warning Undefined variable, looking at the html you have the following inputs:

<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 actually have dollar signs $, so it’s right to use it like this:

$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'];

Browser other questions tagged

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