inserting data into mysql database using php

后端 未结 7 641
生来不讨喜
生来不讨喜 2020-12-22 01:24

I have a php order form named (order.php) and when the user clicks the (submit button \"Next Step\") it takes him to another page called (confirm-order.php)

The (con

7条回答
  •  一整个雨季
    2020-12-22 01:58

    There are two specific things I can contribute.

    First, isset tests for null... which is different than empty. If you have a form field that is submitted empty, then set a local variable to that posted value, then test it with isset; isset will return true because the value exists which is different than the variable not having been registered in the page load at all.

    Second... ANYTHING can post to your form (think evil autonomous Korean hacker bots). Also, there are many ways a form can get submitted without having activated the submit button itself so there is no guarantee you will even see a submit key in your $_POST vars. What you need to define in your processing script is a "default action". What I mean by that is a very basic and SAFE behavior (like redirecting to a 'something is wrong' page) that kicks off by default such that the only way around it is to submit a correct form with all anticipated values correctly set.

    If you do this, you can ignore the value of the submit button itself and instead focus on the contents of the POST. Did I receive everything I expected to receive? Was it all in the correct format? Was the user authenticated correctly? Only after all these questions have been tested to your satisfaction would you switch from the default behavior to a form processing behavior in which the posted data can be inserted into your database.

    Example using your 3 page structure: reference: filter vars

    Page 1:

    Page 2:

     FILTER_VALIDATE_INT
                 ,'stringValue' => FILTER_SANITIZE_STRING);
    
    $clean_data = filter_input_array(INPUT_POST,$args);
    
    if (is_array($clean_data))
    {
      $_SESSION["saved_clean_data"] = $clean_data;
    }
    else 
    {
      Header();
      die();
    }
    ?>
    

    Page 3:

    );
      die();
    }
    ?>
    

提交回复
热议问题