0

i am trying to go to next page but it is currently showing "Notice: Undefined index: u...y". The values i am accessing was accessible before but now it is showing this error.. i have tried to understand the problem but all in vain.. kindly help thanks..

 <?php
$con = new mysqli("localhost","root","","mydb");



$titl = $_POST["u"];
$auth = $_POST["v"];
$isb = $_POST["w"];
$pub = $_POST["x"];
$keyw = $_POST["y"];

$q = "select * from mytable";   
$rs = $con->query($q);  

while($r = $rs->fetch_assoc())
{
    if($titl == $r["title"] && $auth == $r["author"] && $isb == $r["isbn"] && $pub == $r["name"] && $keyw == $r["keywords"])
    {
        echo"<html>
            <body>
            <style>
            body  
            {
                background-image: url('load1.gif');
                background-color: #cccccc;
            }
            </style>
            </body>
            </html>

            ";
            sleep(5);
             header("Location: info.php");
            exit;
    }

    else
    {
        if($check == 'notfound')
        {
        //  echo "Not Found...";
        }
    }
    echo"<br>";

  echo "</tr>";


}
echo "</table>";    
$con->close();
?>
Hassaan Ahmad
  • 29
  • 1
  • 1
  • 8
  • Your `$_POST` does not contain `u`, `v`, `w`, `x` and `y`. Your form does not send the data. You should check that they are defined rather than always assuming they are. – Arcesilas May 23 '16 at 02:43
  • My `$_POST` was working perfectly fine and was receiving values before. It contained u, v, w, x and y values but "now" it is showing the error. this is what i am mainly confused about. – Hassaan Ahmad May 23 '16 at 02:47
  • Before what? Before some magic happened? – Arcesilas May 23 '16 at 02:48
  • Notice that the error you get is a Notice. That mean it can be switch on or off based on your php configuration. I suspect that you turn it off before, hence it's not showing but accidentally turn it on now so it show up. Anyway, dont just count on magic, always check your code first. – Dat Pham May 23 '16 at 02:49
  • The data is not sent by the form, or the error message would not be displayed. Period. You have to investigate in your code that you don't share with us to understand why the form does not send the POST data. – Arcesilas May 23 '16 at 02:50
  • this is what i am confused about. after adding `sleep(5)` and `header();` the error came in an instant. – Hassaan Ahmad May 23 '16 at 02:50
  • Why has this question been marked as duplicate of another one which has nothing in common? – Arcesilas May 23 '16 at 02:53
  • @HassaanAhmad If you don't provide the code of the form that you submit, NOBODY on earth can help you. Unless they have magic powers (mine worked before, but not now) – Arcesilas May 23 '16 at 02:54
  • i dont know either why it has been marked.. let me provide the code from where the values are being passed.. – Hassaan Ahmad May 23 '16 at 02:56
  • `

    Enter the "title" of the book &nbsp&nbsp&nbsp

    Enter the "author" of the book

    Enter the "ISBN" of the book

    Enter the name of the "publisher" of book

    Enter the "keywords" of the book

    `
    – Hassaan Ahmad May 23 '16 at 02:59
  • Please, edit your question, code in comments is unreadable... – Arcesilas May 23 '16 at 03:00
  • this is the code from values are being sent. sorry it wasn't fitting in 550+ lines so had to write in that way. the procedure above is all same. – Hassaan Ahmad May 23 '16 at 03:01
  • ???? It as too long, so you paste it in comments? – Arcesilas May 23 '16 at 03:03
  • Hey you know what? Personally, I give up. You make no effort in debuging your own code, you don't help us helping you... Good luck. I've tried. – Arcesilas May 23 '16 at 03:05
  • `
    Enter Title Enter author Enter ISBN Enter name Enter keywords `
    – Hassaan Ahmad May 23 '16 at 03:07
  • sorry i am trying but i am not able to guide you well. i'll just figure out something else. well thank you for your time and sorry for wasting yours. Glad you helped :) – Hassaan Ahmad May 23 '16 at 03:11

1 Answers1

1

It's to do with the way you're expecting $_POST data to be set. It's either not being set by the form submission or you're trying to access this page directly - which means you have no post data.

General practice is to only run processing like this if there is $_POST data set:

if(isset($_POST['submit'])) {
    $titl = filter_input(INPUT_POST, 'u');
    $auth = filter_input(INPUT_POST, 'v');
    $isb = filter_input(INPUT_POST, 'w');
    $pub = filter_input(INPUT_POST, 'x');
    $keyw = filter_input(INPUT_POST, 'y');

    //...rest of code
}

As you see above we've used filter_input() to manage our POST data being sent. This is how your data will be returned/set:

  • If POST field is set, will be returned.
  • If the filter fails, your variable will be false
  • If the POST is not present, your variable will be NULL

The above would be based off a form like:

<form action="your_page.php" method="POST">
    <input type="text" name="u">
    ...rest of the inputs...
    <input type="submit" name="submit" value="Submit">
</form>

Notice the submit input type (name attribute) - along with each of the inputs having a name attribute.

Darren
  • 13,050
  • 4
  • 41
  • 79