-1

Hi i'm totally beginner with php. So I'm trying this one for my school project. And I'm seeing

Undefined index: n1 in C:\xampp\htdocs\assignment.php on line 49

Really don't know where the problem is.

<html>

<body style="color:Midnightblue;"bgcolor=#eee8aa>
<form action="index.php" method="post">

<h1>Mobile Shop Management</h1>

<font size='16'><i>Type your Name:</i></font> <input type="text" name="n1" size="10" style="background-color: lightblue;    
    display: inline-block; 
    margin-right: 20px; 
    width: 380px;
    height: 50px;
    border-radius: 0%;
    margin-left: 72px;  
    text-align: center;
    font-size: 2em;"><br><br>
<font size='16'><i>Type your Budget:</i></font> <input type="text" name="n2" size="10" style="background-color:lightblue;   
    display: inline-block; 
    margin-right: 20px; 
    width: 180px;
    height: 50px;
    border-radius: 0%;
    margin-left: 50px;  
    text-align: center;
    font-size: 2em;"><br><br>

<input type="submit" size="10" value="Show" style="background-color:lightblue;

    box-shadow: 5px 5px 5px #888;   
    display: inline-block; 
    margin-right: 20px; 
    width: 280px;
    height: 50px;
    border-radius: 0%;
    margin-left: 230px; 
    color: #CC0000;
    text-align: center;
    font-weight: bold;
    font-size: 2em;
    border: 4px solid #008800;">
</form>

</body>
</html>


<?php
if ($_POST['n1']<>""||$_POST['n2']<>"")
{

    $x=$_POST['n1'];
    $y=$_POST['n2'];

$c=mysql_connect("localhost","root",""); 
mysql_select_db('mis410');


$q=mysql_query("select Mobile_brand from mobile_shop where price<=$y"); 



echo "Hello mr./ms. $x <br> Available devices: $q";


mysql_close($c);
}
?> 
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • `mysql_*` functions are deprecated since PHP 5.5 (and **removed entirely** in PHP 7) and you should [stop using them](http://stackoverflow.com/q/12859942) if you can. You should choose another API that allows you to use [prepared statements](http://stackoverflow.com/q/60174/) (which you *really should* when dealing with variables), like `mysqli_*` or PDO - see [choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Qirel Dec 15 '16 at 11:06
  • You should check if the form was submitted at all. When you load the form for the first time, there is no value in the `$_POST` – Shadow Dec 15 '16 at 11:07
  • 1
    The error comes from `if ($_POST['n1']<>""||$_POST['n2']<>"")` - when the form isn't submitted, those indexes doesn't exist. You can also use the native `empty()` function to compare, which will also rid you of this error in a single sweep, making it `if (!empty($_POST['n1']) || !empty($_POST['n2'])) {` (although I suspect you might want AND `&&` instead of OR `||`?) – Qirel Dec 15 '16 at 11:08
  • You need to check if the form is submitted first – Masivuye Cokile Dec 15 '16 at 11:11
  • My knowledge about php is so poor that, I didn't understand anything. :( – Hojaief Ahmed Dec 15 '16 at 11:12
  • I can see the form, the problem I'm having with if ($_POST['n1']<>""||$_POST['n2']<>"") { $x=$_POST['n1']; $y=$_POST['n2']; Whenever I run the file. I see Notice: Undefined index: n1 in C:\xampp\htdocs\assignment.php on line 49 Notice: Undefined index: n2 in C:\xampp\htdocs\assignment.php on line 49 Below. and php is not working. – Hojaief Ahmed Dec 15 '16 at 11:13
  • Read [my comment above](http://stackoverflow.com/questions/41162581/undefined-index-n1-in-c-xampp-htdocs-assignment-php-on-line-49#comment69526000_41162581), it explains why and how to solve it :-) – Qirel Dec 15 '16 at 11:14
  • 1
    IT worked !!!!!!!!!!!!!!!!!!!!!!!!! Thanks Qirel..........you are great. – Hojaief Ahmed Dec 15 '16 at 11:17
  • and you shouldn't be using mysql you should use mysqli with prepared statements – Masivuye Cokile Dec 15 '16 at 11:19

1 Answers1

0

You are posting your form to index.php and are trying to get the variable in assignment.php. Try changing

 <form action="index.php" method="post"> 

to

<form action="assignment.php" method="post">
mihaela
  • 394
  • 2
  • 12