M
M
Maxim2018-07-19 03:07:26
PHP
Maxim, 2018-07-19 03:07:26

How to fill a form via Python3 and send data to db?

You need to send data to the form on the site through the requests library. Then the received data is stored in a table in the database.
Here is the form code:

<form action="" method="post">
        
            <label>Логин</label>
            <input name="login" id="login" type="text"><br/>
            <label>Пароль</label>
            <input name="password" id="password" type="password"><br/>
    </form>

Here is the php code:
if ($_SERVER["REQUEST_METHOD"] == "POST"){
         
    $login = $_POST["login"];
    $password = $_POST["password"];

    $mysqli = new mysqli ("url.ru","user","password","table");
    $mysqli->query("SET NAMES 'utf8'");
    
    $succ = $mysqli->query("INSERT INTO `auth`(`login`,`password`) VALUES ($login,$password)");
    
    echo $succ;
    $mysqli->close();

Here is the python code.
import requests
data = {'login':'test', 'password':'test'}
url = 'url.ru'
requests.post(url, data)

The problem is that python fills out the form, php starts processing the data (this can be seen if the python request is enclosed in a variable, and then see the result through content), but the data is not sent to the database table. If you do the same in the browser (enter data into the form), then everything will be successfully written to the table. Why is this happening and what can you try?
ps You must use requests

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question