0

The php code below echoes the image by using the variables $cat & $var, but when I fill the form & press submit, it shows following error redirecting to the same page instead of going to insert.php. The error is :

Notice: Undefined index: number in C:\xampp\htdocs\k\show_results_4.php on line 161

Notice: Undefined index: category in C:\xampp\htdocs\k\show_results_4.php on line 162

Notice: Undefined index: in C:\xampp\htdocs\k\show_results_4.php on line 164

**

    <?php  
                       $var=$_GET['number'];                 //line 161
                        $cat=$_GET['category'];              // line 162
                        $unipath= array('breads','fruits','milk','vegetables');
                        echo $unipath[$cat];                  //line 164
                      echo '<img alt="140x140" src="/k/images/'.$unipath[$cat].'/'.$var.'.jpg" class="img-rounded"/>';


        if(isset($_SESSION['stats']))
            $mode=$_SESSION['stats'];
            if($mode=='vendor')
            {
                echo "<form method='POST' action='insert.php'>
                    Enter price per item/kg:<input type='text' name='pr'>
                    <input type='hidden' name='vegid' value='".$var."'>
                    <input type='hidden' name='vegcat' value='".$cat."'><br/>
                <br/><button type='submit' class='btn btn-primary'> SUBMIT </button>
                </form>";
                        ?>

For reference the insert.php is :

<?php
include 'conf.inc.php';
session_start();
if(!isset($_SESSION['usr']))
header('Location:login.php?t=1');
$id=$_SESSION['id'];
$vegid=$_POST['vegid'];
$vegcat=$_POST['vegcat'];
if(isset($_POST['pr']))
    {
        $price=$_POST['pr']; 
        if(!empty($price))
        {
            $dur=date('d m Y',time());
                        $query="insert into daily values('".$dur."','".$vegid."','".$vegcat."','".$id."','".$price."')";
                        if($query_run=mysql_query($query))
                        {
                                header('Location:Homepage_2.php?suc=1');
                        }
                        else
                        {
                                header('Location:Homepage_2.php?suc=2');
                        }
                }
            else
            {
        header('Location:Homepage_2.php?suc=3');
        }
        echo mysql_error();
}
?>
  • 1
    `$var=isset($_GET['number']) ? $_GET['number'] : '';` – Funk Forty Niner Jun 17 '15 at 03:21
  • 1
    Form which you submit has method GET or POST? I guess its POST and you are trying to access via GET variable?? – Saravanan Sampathkumar Jun 17 '15 at 03:57
  • When looking at your source code in the browser, do you see any issues with the form code? Many browsers such as firefox etc. will show source code in red if there is an HTML error. I am wondering if perhaps you have another element somewhere (maybe even another form element) that was not properly closed. – kojow7 Jun 17 '15 at 04:01

2 Answers2

0

Change this

<?php
    if(isset($_POST['submit']))
    {
        $var=$_GET['number'];
        $cat=$_GET['category'];
        $unipath= array('breads','fruits','milk','vegetables');
        echo $unipath[$cat];
        echo '<img alt="140x140" src="/k/images/'.$unipath[$cat].'/'.$var.'.jpg" class="img-rounded"/>';

        if(isset($_SESSION['stats']))
            $mode=$_SESSION['stats'];
        if($mode=='vendor')
        {
            echo "<form method='POST' action='insert.php'>
            Enter price per item/kg:<input type='text' name='pr'>
            <input type='hidden' name='vegid' value='" . $var . "'>
            <input type='hidden' name='vegcat' value='" . $cat . "'><br/>
            <br/><button type='submit' class='btn btn-primary' name='submit'> SUBMIT </button>//add name field
            </form>";
        }
    }

?>

Note : you are sending data through POST method. So assign your variables with $_POST. (Ex: $var=$_POST['number']; and $cat=$_POST['category'];)

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

They are not errors, they are notices....

Notice: Undefined index: number in C:\xampp\htdocs\k\show_results_4.php on line 161

You are trying to retrieve the value of $_GET['number'] without check if $_GET['number'] exists

Notice: Undefined index: category in C:\xampp\htdocs\k\show_results_4.php on line 162

You are trying to retrieve the value of $_GET['category'] without check if $_GET['category'] exists

Notice: Undefined index: in C:\xampp\htdocs\k\show_results_4.php on line 164

Since you are trying to get access to $unipath[$cat] and $cat is not set you are trying to get the value of $unipath[] (no index)

In order to use any index value, please check if it exists first.

How?

$variable = isset($array['index']) ? $array['index'] : 'default';
Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33