Python Forum

Full Version: Error using mariadb select query with form in python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to use form with mariadb in python, i want to pass the id from form to mariadb select query to print the selected table row, this is index.html
<form action = "/cgi-bin/var.py" method = "get">
Row Id: <input type = "number" name = "idn"> <br />
<input type = "submit" value = "Submit" />
</form>

This is var.py inside the /srv/http/cgi-bin:
#!/usr/bin/python
import mariadb
import sys
import configparser
import cgi
import cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
mydb = mariadb.connect(
  host="localhost",
  user="user",
  password="",
  database="mail"
  )
mycursor = mydb.cursor()
query ="SELECT * FROM pop where id=%s"
val = form.getvalue('idn')
mycursor.execute(query, val)
myresult = mycursor.fetchall()
for row in myresult:
    print("id = ", row[0])
    print("Server = ", row[1])
    print("User = ", row[2])
    print("Password  = ", row[3], "\n")
    mycursor.close()
    mydb.close() 
After run the code get this error:
Server error!

The server encountered an internal error and was unable to complete your request.

Error message:
End of script output before headers: var.py

If you think this is a server error, please contact the webmaster.
Error 500
example.com
Apache/2.4.48 (Unix) mod_fcgid/2.3.9 mod_wsgi/4.8.0 Python/3.9 
This is the apache error log:
[Wed Jul 28 15:52:42.397653 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215: Traceback (most recent call last):: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397730 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:   File "/srv/http/cgi-bin/var.py", line 5, in <module>: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397747 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:     import cgi: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397776 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:   File "/srv/http/cgi-bin/cgi.py", line 8: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397826 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:     print "Content-Type: text/plain;charset=utf-8\\n\\n": /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397839 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:           ^: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397903 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215: SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Content-Type: text/plain)?: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.403522 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] End of script output before headers: var.py, referer: http://example.com/test/index.html
There was another python code in the cgi-bin causing the error of "Missing parentheses" i remove that code from the cgi-bin now when run the index.html get the same error in the web browser but the error log now did not print any error messages.
I made some changes to the var.py now when run the code get this stuff in the web browser:
4 --> -->
 
 
ProgrammingError	Python 3.9.6: /usr/bin/python
Thu Jul 29 16:51:21 2021

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
 /srv/http/cgi-bin/var.py in <module>
     19 mycursor = mydb.cursor()
     20 query ="SELECT * FROM pop where id=%s"
=>   21 mycursor.execute(query, val)
     22 myresult = mycursor.fetchall()
     23 for row in myresult:
mycursor = <mariadb.connection.cursor object>, mycursor.execute = <built-in method execute of mariadb.connection.cursor object>, query = 'SELECT * FROM pop where id=%s', val = '4'

ProgrammingError: 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 '%s' at line 1
      args = ("You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1",)
      errmsg = "You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1"
      errno = 1064
      msg = "You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1"
      sqlstate = '42000'
      with_traceback = <built-in method with_traceback of ProgrammingError object>