How to insert a new user programmatically to my forum? - mysql

I have a xamarin.forms mobile app, and i have mybb forum.
Is it possible that when a user is creating a new account in the app, user can login to the website with the same credential?
what is the query for doing such a thing?
insert into mybb_users values(............); < what is the MUST INSERT fields
I will only use use email+Password+name (Other fields I will make them null or default value)
Example:
INSERT INTO mybb_users (username, password,email)
VALUES
('username','password','abc#abc.com');
Or should i insert other fields to null or default value
authentication is already done inside the mobile app.
UPDATE:
I have insert a user into mybb_users tables successfully
but when i try to login using the username, i got error message saying user is not registered ??
How do i know what other needed tables i have to fill
Update 2:
This is the registration Page: member.php

You shouldn't need to define all of the columns as long as you are fine with them being null.
See MySQL Insert references for more info.
To answer your second question, you will need to look at the table and the login form itself to see what fields they need. Insert values into those columns.

Related

Check for duplicates across two columns sql

I am currently designing a mysql database that contains emails, and I am trying to find the best way to store said emails. I have read in articles and some stackoverlow posts that it is a good idea to store the local part of an email address separately to the domain, as many emails use the same domain (e.g. gmail.com). Currently I have set up a table that is named emails that contains an id, local_email, and domain_id, the domain_id being a foreign key for a table containing email domains. According to what I found this is the best way to set up a database as it minimises the storage used in repeating email domains.
So far this seems to work very well, however the one problem I seem to be having is that when I want to add a new email, what is the best way to ensure that a duplicate is not being added. Normally I would use the UNIQUE constraint, but since the local part and domain of the email are split up into two different columns, I am unable to do that. So my question is, is there a way to check if an exact email already exists on the database side or do I have to do that at an application level, and if I do, will I not have a problem with the race condition (I know that this is unlikely but would still prefer to not introduce bugs lik that).
I am fairly new to database design so any help is welcome. Thank you.
The Impaler gave you a really good idea by using CONSTRAINT uq1 UNIQUE (local, domain) . Apart from that, we can also use a trigger to check for duplicate values, which provides you with customizations such as returning an error message of your choice . Assuming we have four entries in the domains table using insert into domains (domain_id,domain_name) values(1,'gmail.com'),(2,'yahoo.com'),(3,'yahoo.co.nz'),(4,'msn.com'); and the emails table is empty at the moment. Then we create a trigger which checks for the uniqueness of new rows before they are inserted. If the uniqueness is violated, an error with a message is signaled.
delimiter //
drop trigger if exists check_duplicate ;
create trigger check_duplicate before insert on emails for each row
begin
if new.local_email in (select local_email from emails where domain_id=new.domain_id) then
signal SQLSTATE VALUE '99999' SET MESSAGE_TEXT = 'duplicate address detected';
end if;
end//
delimiter ;
After creating the trigger, we move on to test against it using the following INSERT statements:
insert into emails (local_email,domain_id) values ('john',2); -- successful
insert into emails (local_email,domain_id) values ('marry',1); -- successful
insert into emails (local_email,domain_id) values ('jack_frost',2); -- successful
insert into emails (local_email,domain_id) values ('jack_frost',1); -- successful
insert into emails (local_email,domain_id) values ('jack_frost',2); -- error message 'duplicate address detected'
insert into emails (local_email,domain_id) values ('jack_frost',3); -- successful

Saving new records filtered by "unique" in mysql

I have a query that runs everytime a user logins. Since this query regards information the user might have third-party updated recently I thought it would be a good idea to turn the user_id + information combo in the table unique. As so, everytime a user tried to save new information it would only save the one information I already didn't have. So, the first query being
INSERT INTO table VALUES ("1","cake"),("1","pie"),("1","bedsheets")
And as the user logins a second time and it being
INSERT INTO table VALUES ("1","cake"),("1","pie"),("1","bedsheets"),("1","chocolate")
It would only save ("1","chocolate") because (id,info) being an unique pair all other would not be inserted. I came upon the realization they all fail if only one fails. So my question is: is there any way to override this operation? Or do I have to query the db first to filter the information I already have? tyvm...
When you use the IGNORE Keyword, so every errors, in the execution are ignored. Example: if you have a duplicate or PRIMARY key error while executing a INSERT Statement, so it will ignored and the execution is not aborted
Use this:
I NSERT IGNORE INTO table VALUES ("1","cake"),("1","pie"),("1","bedsheets"),("1","chocolate");

MySQL - Split up INSERT in to 2 queries maybe

I have an INSERT query which looks like:
$db->Query("INSERT INTO `surfed` (user, site) VALUES('".$data['id']."', '".$id."')");
Basically I want to insert just like the above query but if the site is already submitted by another user I don't want it to then re-submit the same $id in to the site column. But multiple users can view the same site and all users need to be in the same row as the site that they have viewed which causes the surfed table to have 10s of thousands of inserts which dramatically slows down the site.
Is there any way to maybe split up the insert in some way so that if a site is already submitted it won't then submit it again for another user. Maybe there's a way to use UPDATE so that there isn't an overload of inserts?
Thanks,
I guess the easiest way to do it would be setting up a stored procedure which executes a SELECT to check if the user-site-combination is already in the table. If not, you execute the insert statement. If that combination already exist, you're done and don't execute the insert.
Check out the manual on stored procedures
http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
You need to set a conditional statement that asks whether the id already exists then if it does update otherwise insert
If you don't need to know whether you actually inserted a line, you can use INSERT IGNORE ....
$db->Query("INSERT IGNORE INTO `surfed` (user, site) VALUES('".$data['id']."', '".$id."')");
But this assumes that you have a unique key defined for the columns.
IGNORE here will ignore the Integrity constraint violation error triggered by attempting to add the same unique key twice.
The MySQL Reference Manual on the INSERT syntax has some informations on that http://dev.mysql.com/doc/refman/5.5/en/insert.html

Update if the field don't contain a sub value

I record the ip address of visitor and store it into mysql database. If the visitor gets a different ip, it will also update the record. I use the following code to do the update and values in following example is just for testing.
insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','')
ON DUPLICATE KEY UPDATE
ip_address=concat(ip_address,'|','100.100.100.100'),
location=concat(location,'|','china'),
screen_res=concat(screen_res,'|','300x600'),
os=concat(os,'|','IOS'),
brower=concat(brower,'|','firefox')
It works, but now problem comes, how can I check if there is a record in the database? like this: visitor comes again, with ip 100.100.100.100. Mysql don't know there is a record, and it will re-record. How to check that if contains a sub string before insert?
if not exists(select * from visiter_info where ip_address='100.100.100.100')
insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','')
ON DUPLICATE KEY UPDATE
ip_address=concat(ip_address,'|','100.100.100.100'),
location=concat(location,'|','china'),
screen_res=concat(screen_res,'|','300x600'),
os=concat(os,'|','IOS'),
brower=concat(brower,'|','firefox')

use trigger with update and insert query both in mysql

i have login page with user name and password, if the user forgot his/her password then there is a link of forgot password,were one has puts its email id and then the default password will send to its id and it will also update it in table with update query,the problem is that when in future we give this project to client we have truncate whole data and give him empty database, at that time update query won't work at that moment i need to use insert query, so i need a trigger query which fires on insert as well as on update query also later on when data is filled, please help because i never used trigger and i don't know its syntax, please explain me by showing an example
You do not need a trigger. Just run an INSERT INTO...ON DUPLICATE KEY UPDATE query. It will insert a row, but if a row with the same primary key already exists, update the column in the existing row instead.
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html