0

I am trying to create a Python script that will fill some information in the database. For the server side I have used PHP and when I try to submit information using a browser, it works. But when I try to do it using the below Python script, it doesn't.

import requests url_insert = 'http://192.168.1.100/index.php' data_insert = {'fullname':'spiderman', 'ssn':'1234', 'dept':'Security', 'salary':10000, 'homeaddress':'New York', 'btn_save':'Save'} req = requests.post(url_insert, data = data_insert) print(req.text) 

Response:

Connected Successfully.<br><html> <head> <title>RETRIEVE DATA</title> </head> <body> <form action="data.php" method="POST"> <div class="form-group"> <label for="id">Full Name</label> <input type="text" name="fullname" id="fullname" value="" placeholder="FullName"> <br> <br> <label for="id">Social Security Number</label> <input type="text" name="ssn" id="ssn" value="" placeholder="Social Security Number"> <br> <br> <label for="id">Department</label> <input type="text" name="dept" id="dept" value="" placeholder="Department"> <br> <br> <label for="id">Salary</label> <input type="text" name="salary" id="salary" value="" placeholder="Salary"> <br> <br> <label for="id">Address</label> <input type="text" name="homeaddress" id="homeaddress" value="" placeholder="Address"> <br> <br> <input type="submit" name="btn_save" value="Save"> </div> </form> </body> </html> 

HTML CODE:

<html> <head> <title>RETRIEVE DATA</title> </head> <body> <form action="data.php" method="POST"> <div class="form-group"> <label for="id">Full Name</label> <input type="text" name="fullname" id="fullname" value="" placeholder="FullName"> <br> <br> <label for="id">Social Security Number</label> <input type="text" name="ssn" id="ssn" value="" placeholder="Social Security Number"> <br> <br> <label for="id">Department</label> <input type="text" name="dept" id="dept" value="" placeholder="Department"> <br> <br> <label for="id">Salary</label> <input type="text" name="salary" id="salary" value="" placeholder="Salary"> <br> <br> <label for="id">Address</label> <input type="text" name="homeaddress" id="homeaddress" value="" placeholder="Address"> <br> <br> <input type="submit" name="btn_save" value="Save"> </div> </form> </body> </html> 

I am new to this, and any help would be much appreciated.

2
  • What exactly doesn't work for you? could you share the full traceback? Commented Apr 18, 2020 at 10:01
  • Your post code is on data.php and your URL you have put it in has index.php in python code. That's initial finding but it would be good if you can share more details. Commented Apr 18, 2020 at 10:04

1 Answer 1

2

I am going to work on the assumption that when you have had success submitting the information using a browser that it was by using the form generated by script index.php.

That HTML form inputs data from a user into field names such as ssn and posts the data to script data.php. Your Python script should likewise be posting the data to data.php (it, however, seems to be missing data for fullname). But instead you are posting to index.php. I would then expect the response to be the HTML form with which you have previously had success submitting the information. That certainly seems to be the case.

Just change index.php to data.php and provide a value for fullname.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it kind of worked. But now I get another error: INSERT INTO emp_record (ename, ssn, dept, salary, homeaddress) VALUES ('', '', '', , '')<br>You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' '')'
I should also add in advance of your nest SQL-related question that if you are going to be taking input from "the outside", e.g. user input, you should be using prepared statements or taking some other measure to ensure that you are not leaving yourself open to a SQL Injection attack. If you do not know what that is, you need to look this up immediately.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.