Add form info into SQLite database python - python

I am a beginner at databases and I want to make a form in my website that requires any user's information. So the user needs to write in their name and email, add a description to an image and upload the image itself. I want to store the information into an SQLite table in Python.
This is my form:
<form role="form" method="post" id="reused_form" enctype="multipart/form-data">
<div class="form-group">
<label for="name">
Name:</label>
<input type="text" class="form-control"
id="name" name="name" required maxlength="50">
</div>
<br>
<div class="form-group">
<label for="email">
Email:</label>
<input type="email" class="form-control"
id="email" name="email" required maxlength="50">
</div>
<br>
<div class="form-group">
<label for="name">
Message:</label>
<textarea class="form-control" type="textarea" name="message"
id="message" placeholder="Your Message Here"
maxlength="6000" rows="7"></textarea>
</div>
<br>
<div class="form-group">
<label for="name">
Image Upload:</label>
<input type="file" class="form-control" id="image" name="image" required>
</div>
<input type="submit" name="my_button" value="Submit">
</form>
I've started with this python code:
connection = sqlite3.connect('database1.sqlite')
cur = connection.cursor()
cur.execute('DROP TABLE IF EXISTS Artpieces')
cur.execute('CREATE TABLE Artpieces ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL, description TEXT NOT NULL, photo BLOB NOT NULL)')
I don't know how to continue. The image part is pretty difficult and I found a tutorial, but I doesn't say how to store an image from a user (so not me). Here it is: https://pynative.com/python-sqlite-blob-insert-and-retrieve-digital-data/ .
Does anyone have any ideas?

Related

Update the password if the user has forgotten while Login-in, Python Flask

I am trying to update the password if the user has forgotten while Login-in. But continuously it is giving the same error given below.
Bad Request: The browser (or proxy) sent a request that this server could not understand.
app.py
#app.route('/NewPassword', methods=['POST', 'GET'])
def NewPassword():
if request.method == 'POST':
OldEmail = request.form['NewPswdInputEmail']
OldPhone = request.form['NewPswdInputPhoneNo']
NewPaswd = request.form['NewPaswdInputPassword']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute("UPDATE USERS SET Paswd='" + NewPaswd + "' WHERE Email ='" + OldEmail + "' OR Phone ='" + OldPhone + "'")
cur.connection.commit()
return "Password Updated"
return render_template('NewPassword.html')
NewPassword.html
<form action="/NewPassword" method="post" oninput='NewPaswdConPassword.setCustomValidity(NewPaswdConPassword.value != NewPaswdInputPassword.value ? "Password does not match." : "")'>
<div class="form-group">
<label for="customRadio">Select Option to Change Password Between Email & Phone No.</label>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="customRadioInline1" name="customRadioInline" value="Email" class="custom-control-input" checked>
<label class="custom-control-label" for="customRadioInline1">Email Id</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="customRadioInline2" name="customRadioInline" value="Phone" class="custom-control-input">
<label class="custom-control-label" for="customRadioInline2">Phone No.</label>
</div>
</div>
<div class="form-group" id="EmailShowHide">
<label for="NewPswdInputEmail">Email Address</label>
<input type="email" class="form-control" name="loginInputEmail" id="NewPswdInputEmail" aria-describedby="emailHelp" placeholder="Email address">
<small id="loginemailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group" id="PhoneShowHide" style="display: none">
<label for="NewPswdInputPhoneNo">Phone Number</label>
<input type="tel" id="NewPswdInputPhoneNo" name="NewPswdInputPhoneNo" class="form-control" pattern="[0-9]{4}[0-9]{2}[0-9]{4}" placeholder="Phone number">
<small id="NewPswdPhoneHelpInline" class="text-muted">Enter Valid Number</small>
</div>
<div class="form-group">
<div class="tool">
<label for="NewPaswdInputPassword">New Password</label>
<div class="input-group" id="NewPaswdShowHide">
<input type="password" class="form-control" name="NewPaswdInputPassword" id="NewPaswdInputPassword" pattern="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,20}$" placeholder="New password" required>
<div class="input-group-append">
<a class="btn btn-primary">
<i class="bi bi-eye-slash" aria-hidden="true"></i>
</a>
</div>
</div>
<span class="tooltext">Password Must be Alphanumeric with atleast an UpperCase & LowerCase Characters</span>
<small id="NewPaswdHelpInline" class="text-muted">Must be 8-20 characters long.</small>
</div>
</div>
<div class="form-group">
<label for="NewPaswdInputConPassword">Confirm New Password</label>
<input type="password" class="form-control" name="NewPaswdConPassword" id="NewPaswdInputConPassword" placeholder="Confirm new password" required>
</div>
<button type="submit" name="Login" id="NewPaswdSubmit" class="btn btn-primary">Submit</button>
</form>
I am trying to do is, if my database "USERS" has Email or Phone no. then Paswd will get updated with the new password which is input by the user
Please tell me what I am doing wrong.
I assume you are using Flask. Is your application running in debug mode?
Running the application in debug mode can help identify where the issue is.
The issue is with the email HTML form input. When you submit a form, the data is sent as key=value pairs, in this case you are using Flask so it is a python dictionary. So in the /NewPassword route, when you request request.form['NewPswdInputEmail'], you are grabbing a value in that dictionary submitted from the form with 'NewPswdInputEmail' as a key.
In the form input the key is the name attribute.
That's why you get the KeyError because the name in the email input form has name="loginInputEmail" instead of name="NewPswdInputEmail"
All you need to do is rename loginInputEmail attribute in the email input field to NewPswdInputEmail.

how to write where clause to select multiple values for same column in flask mysql with checkbox

Flask code of python
cursor = conn.cursor()
amin=request.form.getlist('interest')
str1 = ','.join(amin)
cursor.execute("SELECT * FROM emp_vehical_report WHERE Location IN (%s)",[str1])
vehical_details=cursor.fetchall()
rendered=render_template('Download.html',list=vehical_details,x=str1)
I need to get all values from database with the checkedbox location
But I get it for only one location and not for other selected checkboxes
basically [str1] has values ['Building1,Building2'] if 2 checkboxes selected
of which query doesnt work
else [str] has ['Building1'] if 1 checkbox is selected and it works
Help me with multiple checkboxes
Thanks in advance
HTML code
<div>
<input type="checkbox" id="Building1" name="interest" value="Building1">
<label for="Building1">Building 1</label>
</div>
<div>
<input type="checkbox" id="Building2" name="interest" value="Building2">
<label for="Building2">Building 2</label>
</div>
<div>
<input type="checkbox" id="Building3" name="interest" value="Building3">
<label for="Building3">Building 3</label>
</div>
<div>
<input type="checkbox" id="Building4" name="interest" value="Building4">
<label for="Building4">Building 4</label>
</div>
<div>
<input type="checkbox" id="Building5" name="interest" value="Building5">
<label for="Building5">Building 5</label>
</div>
<div>
<input type="checkbox" id="AL" name="interest" value="AL">
<label for="All Locations">All Locations</label>
</div>

How would I send and request data to a web server using python?

I've been trying to use the request library in order to send information to a web server. However, I have no clue what data to send to the server in order to log in. (This is my first time using requests and I was wondering if someone could help explain this to me)
Edit: I'm trying to make it submit a username and password to the site.
My code:
import requests
Sdata = {'username':'testingemail#gmail.com','password':'testing123','login':'submit'}
r = requests.post(url = 'https://gmchosting.com/billing/clientarea.php',data=Sdata)
html = r.text
print(html)
The Part of the website I want the file to communicate with:
<input type="hidden" name="token" value="003cc13dfa662dd183f098c2a2afc81331da0ba3" />
<div class="form-group">
<label for="inputEmail">Email Address</label>
<input type="email" name="username" class="form-control" id="inputEmail" placeholder="Enter email" autofocus>
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" name="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="rememberme" /> Remember Me
</label>
</div>
<div align="center">
<input id="login" type="submit" class="btn btn-primary" value="Login" /> Forgot Password?
</div>
</form>
</div>
You can use parameters in the url, like this:
https://google.com?myparameter=true
And you can get them using JS.
So, you can use:
import webbrowser
webbrowser.open('https://example.com?myparam=hello&hello=yeah')
And in the JS:
const params = new URLSearchParams(window.location.search);
const myparam = decodeURIComponent(urlp.get('myparam'));
alert(myparam)

django request getlist method: does this preserve the sequence of my posted parameters?

I am posting a sequence of image files and the corresponding description for each image. So the skeleton html form part:
<div class="form-group">
<input type="file" name="image">
<input type="file" name="description">
</div>
<div class="form-group">
<input type="file" name="image">
<input type="file" name="description">
</div>
<div class="form-group">
<input type="file" name="image">
<input type="file" name="description">
</div>
My server is Django 1.6.11 and I want to use django's getlist for image param and description param. So I want to make sure that the two lists I got--image_list and description_list do match, meaning that image_list[0] indeed matches with description_list[0], image_list[1] indeed matches with description_list[1] and image_list[2] indeed matches with description_list[2].
Based on what I see when tests with Chrome and Django 1.6.11, it seems that the corresponding relationship is guaranteed. However, I am not sure if this applies all the time.
I don't think you should rely on the sequence of getlist. Instead you should give different names to each <input>:
<div class="form-group">
<input type="file" name="image0">
<input type="file" name="description0">
</div>
<div class="form-group">
<input type="file" name="image1">
<input type="file" name="description1">
</div>
<div class="form-group">
<input type="file" name="image2">
<input type="file" name="description2">
</div>
Then in your views you could do:
for i in range(0, 3):
file = request.POST['image%s' % i]
description = request.POST['description%s' % i]
# other operations

How to post data via form HTML?

I want to post the data to the server via form. How can I do this?
<div class="panel-body">
<form role="form" action="{% url 'login' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="sender-email" class="control-label">Username:</label>
<div class="input-icon"> <i class="icon-user fa"></i>
<input id="sender-email" type="text" placeholder="Username" class="form-control email">
</div>
</div>
<div class="form-group">
<label for="user-pass" class="control-label">Password:</label>
<div class="input-icon"> <i class="icon-lock fa"></i>
<input type="password" class="form-control" placeholder="Password" id="user-pass">
</div>
</div>
<div class="form-group">
<input class="btn btn-primary btn-block" id="authenticate_user" type="submit" value="Submit">
</div>
</form>
</div>
My views.py
#csrf_exempt
def signin(request):
if request.method == 'POST':
print request.POST.get('sender-email') #prints None
username = request.POST['username'] # throws error, username not available.
password = request.POST['password']
How do I post the data without using jquery? I know of a way of posting through AJAX jquery, but dont wanna do that.
The 'name' attribute of input tags is set as keys of request.GET and request.POST.
Add name attribute to all your input tags.
<input name="username" id="sender-email" type="text" placeholder="Username" class="form-control email">
<input name="password" type="password" class="form-control" placeholder="Password" id="user-pass" >
POST data are populated using the name attribute of your input elements. Change your id attributes or add a name one accordingly. E.g:
<input type="password" class="form-control" placeholder="Password" name="user-pass">
You should also consider using a form to handle your data more easily.

Categories