我是python的初学者,我有简单的代码插入数据库或从数据库中删除用户信息。我使用HTML表单获取用户信息,并使用python代码删除或将用户信息插入数据库。我的HTML表单有两个提交类型按钮添加和删除。
我正在使用if条件来执行操作,具体取决于按下哪个按钮。(Python3.4,Mysql.connector,在localhost中运行)
Python代码:
#!"C:\python34\python.exe"
import cgitb ,cgi
import sys
import mysql.connector
cgitb.enable()
form = cgi.FieldStorage()
first_name = form.getvalue('first_name')
addres = form.getvalue('addres')
print("Content-Type: text/html;charset=utf-8")
print()
#print(form)
conn = mysql.connector.connect(host='localhost',port='8051',
database='tabl',
user='root',
password='new')
cursor = conn.cursor()
if "add" in form:
cursor.execute("""insert into tablename values (%s, %s)""",(first_name,addres))
print("inserted")
elif "delete" in form:
cursor.execute("DELETE FROM tablename WHERE fname=%s and location = %s",(first_name,addres))
print("deleted")
else:
print ("Couldn't determine which button was pressed.")
conn.commit()
如果条件我已经给出了打印声明,如果它被插入或删除。但是它不会打印那些语句而不会给服务器错误。如果我删除那些打印语句它将起作用。
请在上面的代码或其他写作方式中说明错误。
错误:
Server error!
The server encountered an internal error and was unable to complete your request.
Error message:
End of script output before headers: python3.py
If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4