Lesson 101 predefined super global array ① - principle analysis

Keywords: PHP Database SQL MySQL

Interface:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
    <form method="post" action="101_register2.php">
        User name:<input type="text" name="username"><br/>
        Password:<input type="password" name="password"><br/>
        Gender:<input type="radio" name="sex" value="man">male<input type="radio" name="sex" value="women">female<br/>
        <!--Be careful checkbox If you have all of them on the other end, you must do the following name Value with brackets[] In this way, the past system will be processed as an array by default-->
        <input type="checkbox" name="hobby[]" value="Take a shower">Take a shower
        <input type="checkbox" name="hobby[]" value="Sing">Sing
        <input type="checkbox" name="hobby[]" value="Riding">Riding
        <input type="checkbox" name="hobby[]" value="To war">To war
        <br/>
        <!-- size Show several multiple Multiple choices -->
        <select name="aihao">
            <option value="Chinese" selected>Chinese</option>
            <option value="English">English</option>
            <option value="Japanese">Japanese</option>
        </select>
        <br/>
        <textarea name="intr" rows="10" cols="30">Please fill in personal details</textarea>
        <br/>
        <input type="file" name="myphoto" />
        <br/><input type="submit" value="Submission">
    </form>
 </body>
</html>

Receive:

<?php
    header("content-type:text/html;charset=utf-8");//Dealing with Chinese code disorder
    echo "<pre>";
    echo var_dump($_POST);
    echo "<br/>";
    echo $_REQUEST['sex'];
    echo "</pre>";

?>

Effect:

Note: Please note that if the value can be transmitted in English, for example, if the upper gender is written in Chinese, if the value cannot be received by the opposite end, it must be handled. Please

The received data can be stored in the database

<?php
    header("content-type:text/html;charset=utf-8");//Dealing with Chinese code disorder
    echo "<pre>";
    echo var_dump($_POST);
    echo "<br/>";
    //echo json_encode($_POST);//This directly converts the received data into json strings
    if(!empty($_REQUEST['sex'])){
        echo $_REQUEST['sex'];
    }

    echo "</pre>";


    $username = $_POST['username'];
    $pwd = $_POST['password'];
    $sex = $_POST['sex'];
    $hobby = $_POST['hobby'];
    $introduce = implode(",",$hobby);
    //echo "$introduce";
    $aihao = $_POST['aihao'];
    $myphoto = $_POST['myphoto'];
    $mysqli = new MySQLi("localhost",'root','tmdqobn','db100');
    if($mysqli->connect_error){
        echo "Database connection error".$mysqli->connect_error;
        return;
    }
    $sql = "insert into user (username,pwd,sex,aihao,introduce,file) VALUES ('$username','$pwd','$sex','$introduce','$aihao','$myphoto')";
    $res = $mysqli->query($sql);
    if(!$res){
        echo "Add failure";
    }else{
        echo "Add success";
    }
?>

Effect:

mysql> select * from user;
+----------+---------+------+-------------------+-----------+--------------+----
+
| username | pwd     | sex  | aihao             | introduce | file         | id
|
+----------+---------+------+-------------------+-----------+--------------+----
+
| root     | tmdqobn | m    | Take a shower,Sing,Riding,hit | Chinese      | 11111111.png |  1
|
+----------+---------+------+-------------------+-----------+--------------+----
+
1 row in set (0.06 sec)

mysql>

Difference between POST, POST and GET
1. Security
2. The size of data transfer get is small mainly due to browser restrictions
3. Easy to save to Favorites

$_REQUEST - HTTP Request variable

Explain

By default, an array of GET, GET, POST, and COOKIE is included.

Free to receive any one of the above

Note: the official recommendation is not to use this method frequently, which is vulnerable to attack.

Posted by mart on Sat, 25 Jan 2020 07:17:17 -0800