PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How can i save a list into a mysql database?

 October 24, 2022     mysql, python, sql-insert, sql-update     No comments   

Issue

I want to save a list with discord invites into my database. But everytime I ran this code, get's only 1 item saved in the database. I tried to print the inviter ids to check if he just runs one item, but he runs the code for every item. So what is wrong? Why get's only 1 item saved to the database?

            for invite in invites:

                if invite.inviter is None:
                    continue

                if not ctx.author.guild.get_member(int(invite.inviter.id)):
                    continue

                if int(invite.uses) == 0:
                    continue

                await cursor.execute("SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL", (ctx.author.guild.id, invite.inviter.id))
                find_user = await cursor.fetchone()
                if find_user:
                    await cursor.execute("UPDATE guild_invite_count SET real_count = real_count + %s, total_count = total_count + %s WHERE guild_id = %s AND user_id = %s", (int(invite.uses), int(invite.uses), ctx.author.guild.id, invite.inviter.id))
                else:
                    await cursor.execute("INSERT INTO guild_invite_count (guild_id, user_id, real_count, total_count) VALUES (%s, %s, %s, %s)", (ctx.author.guild.id, invite.inviter.id, int(invite.uses), int(invite.uses)))
                print(invite.inviter.id)
                await mydb.commit()


Solution

This SQL

SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL

is logically incorrect. AND user_id = %s IS NOT NULL reduces to AND user_id = true (unless the parameter is null). Just do

SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s


Answered By - snakecharmerb
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing