I have a registration form and 3 database table. Which is users, security,activity. In user there is name,email In security there is token and tokenExpire and in activity there is ip,system.
I want to make insert file where if user submit form it fill that each tables as based places.
Let's consider,
Name=$name | Email=$email | Ip=&ip | token=$token | tokenExpire= $time | system=$system.
I am insert connection like this :
$con->query("INSERT INTO users (Name,Email) value('$name','$email'). system(token,tokenExpire)value('$token','$time).activity(ip,system)VALUE('$ip','$system' ");
Is this correct format?
No, you cannot insert into multiple tables in one MySQL command.
Please try this, I hope it will help you
INSERT INTO users (Name, Email)
VALUES('test', 'test');
INSERT INTO system (user_id,token, tokenExpire)
VALUES(LAST_INSERT_ID(),'token','tokenexpirevalue');
INSERT INTO activity (user_id,ip, system)
VALUES(LAST_INSERT_ID(),'ip','system');
Related
I'm trying to find out if it's possible to insert the data from one table to another table if someone submits an ID that is indexed in the two tables.
For example:
I have Table A with: ID | First Name | Last Name | Course
and Table B has: ID | Name | Course
Table A has data: 32 | John | Doe | IT
And Table B will receive its data from a Form that a user will input.
How do I automatically insert the First Name and Last Name column from Table A into Table B if the user inputs #32 on the form as well which is indexed with Table A ID column?
I'm trying to use the normal Insert Into and Selecting the table columns for the Values of the name but it doesn't seem to read it.
$upload = "INSERT INTO `TableB` ( `ID`, `Name`, `Course`) VALUES ('$id','(SELECT FirstName FROM TableA WHERE ID = '$borrow_id'),' '$course');";
I was thinking that maybe I can select the ID from the submitted form and if there is a similar data in Table A, it will get the other data from it and insert it into Table B.
Your logic is almost there, you just got the syntax wrong. You can use an INSERT..SELECT statement on TableB selecting the data you want from TableA, so it should look something like this:
<?php
$upload = "INSERT INTO `TableB` ( `ID`, `Name`, `Course`) SELECT $id, FirstName, $course FROM TableA WHERE ID = '$borrow_id;";
To test, you can just copy the SELECT part of your query and execute on your database to see if the data is correct. Keep in mind that this statement should have the exact number of columns in your insert and be in the same order.
I recently cleaned up my web app's code. My web app is a simple social network with Add,Remove,etc procedures. I have a php file which I call when a user want to remove a contact from his friend list.
I came up with a strange bug in my application that I haven't estimate the time I wrote it.
The Bug
Let's say we have two friend emails in our list.
georgeMARK#email.com
MARK#email.com
The contact emails are stored in a MYSQL DB column with name Contacts and every email is separated by a semicolon (;) symbol. So our example's column will look like this (depend on which user has added lastly it can be vice versa):
georgeMARK#email.com;MARK#email.com;
So assume that our user wants to remove MARK from his contacts. He presses the button and ... both email's have gone.!
Have a look in my PHPMyAdmin Panel I found that what has left from the column is this:
george
The problem is in my PDO SQL statement that I use to remove accounts from my column.
$STH = $DHB->prepare("UPDATE `Users` SET `Contacts` = REPLACE(`Contacts`, :email, '') WHERE `UserEmail` = :my_email");
The REPLACE function just replaces all the instances of the :email and thats awful. I use the same approach many times in my application and I understand that this will cause my the same bug to appear elsewhere too.
My question is how can I delete a user's email without affecting the others 'instances'? Do I have to change my point of view and how what do I need to do?
The fast dirty way is to prepend a semicolon to all Contacts field, so that entries as like this:
;georgeMARK#email.com;MARK#email.com;
then you can replace it in this way:
$emailToDelete = ";$emailToDelete;"
... REPLACE(`Contacts`, :email, ';') ...
A better approach can be to store contacts data in a more suitable format (like JSON).
The best solution is to create a relationships table and delete all the entries relative to contact to delete.
Structure
Maybe create a structure like this:
Table Users
id | Name | Email
-----------------
1 | John | john#example.com
2 | Jane | jane#example2.com
Table Friendships
User1_id | User2_id
-------------------
1 | 2
Select users
SELECT * FROM Users WHERE name=:name
Select friends
SELECT
*
FROM
Friendships
LEFT JOIN
Users
ON
Friendships.User1_id = Users.id
WHERE
User1_id=:user_id
Delete friendship
DELETE FROM Friendships WHERE User1_id=:current_user_id AND User2_id=:friend_to_remove_id
I have database table kladilnica with 37 values, I want to make the users can send tickets each other, so I have created another table called ticket but that table have 38 values including receiverName so i can easy check who is the receiver and who can see the ticket. I'm going with w3schools example of copying table info and pasting into another table ex. ($sql = "INSERT INTO ticket (senderName, receiverName, Date) SELECT Username, '', Date FROM kladilnica WHERE ticket_id='".$ticketID."'";) this example works well but I have a minor modifications in my example. In the SELECT part i have nothing to select from the kladilnica to get the receiverName and display it into ticket what should I do to get and write the receiverName into ticket table?
$sql = "INSERT INTO ticket (senderName, receiverName, Date) SELECT Username, '".$receiverName."', Date FROM kladilnica WHERE ticket_id='".$ticketID."'";
(and add security for SQL injection of course...)
I have a table name Users and a table name Application.
I want to insert 3 data into 3 columns (id,password,privilege) at Users table.
How do i extract 2 data from Application table (email_address,password) then i add own data in privilege column at Users table.
Sorry if this is confusing.
You can use the following query:
INSERT INTO `Users` (id, password, privilege)
SELECT email_address, password, 'privileges' FROM `Application`
Where privileges stands for the user privileges
I am designing a Diabetes management system and my question is about inserting a new record in two of the tables. I have a user table with the fields:
user_ID (PK), Username, password, etc
I also have the profile table, which stores personal information about a user. Profile:
User_ID (FK from user table), Firstname, LastName, Address, etc.
I have stored the user_id in the profile table as a foreign key because i want to have a user associated with their own profile. E.G. a person with username grover with User_Id '3' will have a profile with personal info and the user_ID in the profile table will also be '3'. I hope the above makes sense. My question is, when i have stored the data from the form into variables, how would i write the query so that the new record will be created and the right fields will be stored in the correct table with the new record having the same user_ID in both the user and profile table.
Try looking at LAST_INSERT_ID function. This will give you the PK of the record you just entered, ie your user_id which you can use in the corresponding join tables
insert into Users values (null,'asdf','asdf',3,1);
Query OK, 1 row affected (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
There you go