INSERT and set a field to copy the newly inserted id - php

Is it possible to INSERT a row, and simultaneously set one of the fields to contain the inserted row's ID? (the "self" id)
I'm trying to avoid using multiple queries if possible (it would be 3 queries in total otherwhise)
Something like this (but probably not):
INSERT INTO thetable (email, phone, activationkey) VALUES ($email, $phone, CONCAT(THIS_NEW_ID, md5($activation) )

Why would you want to store duplicated data?
You could change your table structure and just run:
INSERT INTO thetable (email, phone, activation_suffix)
VALUES ($email, $phone, md5($activation))
And then you have all the data you need.
You can always concat when you query the table:
SELECT CONCAT(id, activation_suffix) activationkey
FROM thetable
WHERE ...
UPDATE
On second thoughts, do you really need the id as part of the activationkey?
I wouldn't want to give any user the id for their record in my table unless it is hashed/encrypted.

Related

how to insert values to a field from another fields

i already input about a thousand values to a table but my client want a "little" revision that ihave to insert a field (fulldetail) is there a way to get the values of the fields (acount, fname, mname, lname) and combind and put into the field (fulldetail).
After updating the table structure (adding the new column), you can execute a mysql query like this:
UPDATE `table_users`
SET `fulldetail` = CONCAT_WS( " ",`acount`,`fname`,`mname`,`lname` )

PHP MySql Insert columns form one table to another

I want to insert my table data from copying another table with some new given data.
I use this query
$sql = "INSERT INTO table2(name, city, email,money)
hasib,SELECT table1.city, table1.email,
newsletter_subscribers.email
FROM table1 WHERE name='jesy',100";
But its not Work
You must give all columns inside the select statement columns, even constant values, e.g.
insert into table2(name, city, email, money)
select 'hasib', city, email, 100
from table1
where name = 'jesy'
If you want to take values from multiple tables, as your select statement suggests, you must look into joins.

How to send data to two tables with same user ID as primary key?

I am totally new to PHP and I'm trying to create a registration page. To register, a user has to create a username, password, email, which are put into a table called users.
They also enter address details which is put into a table called customer_info.
In both tables I have created an auto increment, primary key called 'user_id'.
When the form is completed it fills out and enters the data, but the data is not banded and so there are two user_id, one in users and one in customer_info.
First I create values (from the post) that have been entered and assign them to variables. Then I put the variables into my table using the following query:
$result = mysql_query(
"INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);
and
$result = mysql_query(
"INSERT INTO `customer_info`(firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);
How would I set it so that it creates only one user id for both tables (making a connection between the sets of data)?
Is there something missing in my query, that should connect the tables?
Before anything, you should not use mysql_* extension anymore. Go towards PDO or mysqli
Your technique generates two different unique ids. The point is to have only one, so that it can be unique, and link information on that unique id.
The users table is the one with that unique id, user_id, which is your auto_increment column. The customer_info table can also have a info_id unique column, but must contain a user_id column, which will contain the user's user_id, linking the rows together.
It would also be a great moment to add a foreign key to your tables so that integrity of the data won't be compromised.
so after this query:
$result = mysql_query(
"INSERT INTO `users`(username, password, email) VALUES ('$value1', '$value2','$value3')"
);
get the insert id:
$id = mysql_insert_id();
then run your other query with it:
$result = mysql_query(
"INSERT INTO `customer_info`(user_id,firstname, lastname, b_add_num, b_add_road, b_add_town, b_add_pc, p_add_num, p_add_road, p_add_town, p_add_pc) VALUES ('$id','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12','$value13')"
);
I would configure USER_ID as an AUTO_INCREMENT column only in Users table, insert the data into Users table first, then get the ID of the user inserted, using the mysql_insert_id ($connection_id); and use that while inserting data into Customer_Info table. This way, you can leverage the ID generation (sequence) feature of MySQL as well.

SQL: Get value of a column during update

Using PHP and MySQL, I have a query that will look something like this:
UPDATE mytable
SET status='$newstatus'
WHERE (col1='$col1[0]'AND col2='$col2[0]')
OR (col1='$col1[1]'AND col2='$col2[1]')
OR (...);
I actually need to record the current 'status' of each of these rows before the update. Do I need to do a separate SELECT before this, or can (should / how would) I combine the two queries?
You cannot get that from this query (you could only get number of affected rows, but that's it). If you need that, you shall first do SELECT on your conditions like:
SELECT `id` FROM `mytable`
WHERE (`col1`='$col1[0]' AND `col2`='$col2[0]')
OR (`col1`='$col1[1]' AND `col2`='$col2[1]')
OR (...)
and then do UPDATE with WHERE using fetched ids. I do not recommend doing UPDATE with your current WHERE clause as in meantime (between your SELECT and UPDATE) db content could change, so you could be UPDATING different rows that you had SELECTed. Or use table locking (but I do not think it makes sense here).
No OUTPUT clause in Mysql. You need to either read status prior to update or create a trigger that stores value of OLD.status in other table.
You can't have a single query to update the row and record the current status before updating.
You'd better have a "log table", with the same schema of your "table" plus a timestamp, but it would store only historical data, the status of a row in a single point in time, like a versioning system.
Example:
Table User: Id, Username, Email, Telephone
Table UserLog: Id, Username, Email, Telephone, Timestamp
So, before updating a row on table User, you'd first do a SELECT and an INSERT, like this:
insert into UserLog
select Id, Username, Email, Telephone, Now() from User where Id=$Id

PHP Getting Previous MySQL Query's ID For Next Queries, How?

I have a simple question about MySQL and PHP here. Let's say I have this PHP syntax :
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");
on that Person table, there's a column named ID (Auto Increment). how to get Peter Griffin's ID after INSERT process is done without making another SELECT query?
or is it possible to do INSERT for 2 tables using single query? for example I want to INSERT Peter's address as well on Address table :
mysql_query("INSERT INTO Address (City, State, Zip)
VALUES ('Cupertino', 'California', 35212)");
that's all..
$new_id = mysql_insert_id();
Put that right after your INSERT query and it will give you the id.
I would't recommend trying to do two INSERTs in one, and the mysql_insert_id() method will make it simplest for you and your code.

Categories