0

I have been working on my final project, which is a chat application. I am, however, facing a problem when it comes to sending the messages. I am implementing my project through an SQL database for the messages, and using an html form to get the messages. I simplified the form so that I could try to pinpoint the problem, and here is the html page through which the message is sent:

{% extends "layout.html" %}

{% block title %}
    Chat
{% endblock %}

{% block main %}
    <form action="/chat" method="post">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th></th>
                    <th></th>
                </tr>
            <thead/>
            <tbody>
                {% for message in messages %}
                    <tr>
                        {% if message.class == "sent" %}
                            <td></td>
                            <td>{{ message.text }}</td>
                        {% else %}
                            <td>{{ message.text }}</td>
                            <td></td>
                        {% endif %}
                {% endfor %}
                    </tr>
            </tbody>
        </table>
        <div class="form-group">
            <input class="form-control"  type="text" name="mes" placeholder="Write your message here..."/>
        </div>
        <button class="btn btn-primary" type="submit">Send</button>
    </form>
{% endblock %}

Here is my code for sending the message(recipientall is a global variable for the other person a user is chatting with):

def send():
    from time import gmtime, strftime
    time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    text = request.form.get("text")
    db.execute("INSERT INTO texts(id, recipient, text, time) VALUES(:id, :recipient, :text, :time",
                id=session["user_id"], recipient=recipientall, text=text, time=time)
    return load(recipientall)

and here is the code for load(accessed in send, for loading all the past messages sent between two users). Also, please keep in mind that I have simplified my code for the html page, so some parts of this are not used in the html page code I shared:

def load(recipient):
user = db.execute("SELECT * FROM users WHERE username = :username", username=recipient)
if not user:
    return apology("Invalid recipient!")
    return 1

sent = db.execute("SELECT text, time FROM texts WHERE id = :id AND recipient=:recipient",
                   id=session["user_id"], recipient=recipient)

username0 = db.execute("SELECT username FROM users WHERE id = :id", id = session["user_id"])

username = username0[0]["username"]

ido = db.execute("SELECT id FROM users WHERE username = :recipient", recipient=recipient)

recid = ido[0]["id"]

recieved = db.execute("SELECT text, time FROM texts WHERE id=:recid AND recipient=:username", recid=recid, username=username)

message = {}
messages = []

names = db.execute("SELECT first_name, last_name FROM users WHERE username = :recipient",
                   recipient=recipient)

contact = names[0]["first_name"] + " " + names[0]["last_name"]
myname = db.execute("SELECT first_name, last_name FROM users WHERE id = :id",
                   id=session["user_id"])
me = myname[0]["first_name"] + " " + myname[0]["last_name"]
tosort = []
sort = []
sort += sent
sort += recieved

for sen in sent:
    tosort.append(sen["time"])
for recieve in recieved:
    tosort.append(recieve["time"])
times = sorted(tosort)

for time in times:
    message = {}
    for sen in sent:
        if sen["time"] == time:
            message["text"] = sen["text"]
            message["class"] = "sent"
            messages.append(message)
    for recieve in recieved:
        if recieve["time"] == time:
            message["text"] = recieve["text"]
            message["class"] = "replies"
            messages.append(message)
people = []
per = {}
persons = db.execute("SELECT recipient, time FROM texts WHERE id=:id", id=session["user_id"])
for person in persons:
    recipient = person["recipient"]
    name0 = db.execute("SELECT first_name, last_name FROM users WHERE username=:recipient", recipient=recipient)
    namerec = name0[0]["first_name"] + " " + name0[0]["last_name"]
    per["name"] = namerec
    recid0 = db.execute("SELECT id FROM users WHERE username = :recipient", recipient=recipient)
    recid0 = recid0[0]["id"]
    myus = db.execute("SELECT username FROM users WHERE id=:id", id=session["user_id"])
    myus = myus[0]["username"]
    sen = db.execute("SELECT time FROM texts WHERE id=:id AND recipient=:recipient", id=session["user_id"], recipient=recipient)
    timesen = sen[0]["time"]
    rec = db.execute("SELECT time FROM texts WHERE id=:id AND recipient=:recipient", id=recid0, recipient=myus)
    timerec = rec[0]["time"]
    if timesen >= timerec:
        text = db.execute("SELECT text FROM texts WHERE id=:id AND recipient=:recipient AND time=:timesen",
                            id=session["user_id"], recipient=recipient, timesen=timesen)
        text=text[0]["text"]
        per["text"] = text
    else:
        text = db.execute("SELECT text FROM texts WHERE id=:id AND recipient=:recipient AND time=:timesen",
                            id=recid0, recipient=myus, timesen=timerec)
        text=text[0]["text"]
        per["text"] = text
    people.append(per)

return render_template("message.html", messages=messages, contact=contact, people=people, me=me)

So the problem is showing up the send function. When I try to input a value from the form submission, I get this error message: TypeError: execute() got multiple values for argument 'text' I then tried to hard code a value into the "text" variable, and I recieved the same message, which ruled out the idea that the name "text" from the html form might have had something to do with it. Then I thought it might be picking up on some other "text" variable form application.py, so I changed the name of the variable and still, I got the same result. What might the problem be?

If you have read until here, thank you very, very much for all your help!

1

The problem stems from the bound variable named "text" (ie :text). It is conflicting with something in CS50/sql.py or sqlalchemy, like a reserved word or something. I cannot find the specific cause online, but I did a quick test in my environment After changing :text to :txt it did not produce the error message.

(And fyi the test html is sending parameter named mes not text).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .