PHP Mysql query insert - php

I have a table - 'A' which has primary key - id which is auto incremented.I also have a table - 'B' which also has the column 'id'. Now I want to add entries to the table A so that value of id attribute of table 'A' which just got inserted to the table also gets inserted to B while other attributes of B can remain NULL.I know I can just make one table with all the columns but is there a way to do this with two tables??

Use the mysqli_insert_id () function right after you run the insert to table a. That will give you the value of the auto incremented id from table a which you can then populate in the surrogate key column of table b.
Read about it here:
http://php.net/manual/en/mysqli.insert-id.php

Related

Can I insert data in an auto increment column?

I've created a database in MySQL which has a user_id that is primary key and auto incremented. Now my question is can I insert a static data in a column that is auto incremented? For example I want to insert a data "U-01" instead of just 1 only.
The column must be unauto increment, then you can insert this data!

How to insert row to child table with null value of column that is foreign key

Two Table
First table CUST_ENTRY
cust_id(primary key),cust_name,cust_add
Second table CUST_PRICE
cust_price_id,cust_id(foreign key),cust_price1,cust_date1,cust_price2,cust_date2
I have insert first table completely but second table data cust_price2 and cust_date2 is insert blank but that not possible.
Please help me.

Using php mysql set auto_increment primary value into other field

how can i set id primarykey auto_increment same value that automatically generating into my id field will also generate in other field in same table.
How could it possible?
Use mysql_insert_id which returns last auto increment ID...
1) insert into first table, use mysql_insert_id() to get auto increment ID
2) after getting auto increment ID from 1st table, insert on second table with the ID you got.
for more refer http://php.net/manual/en/mysqli.insert-id.php

How to get MySQL value from certain row?

I have a table with 5 rows. Every time a user enters data into a form, it is entered into the table. My first column is called id and holds the number of the post. What I want to do is get the value of id from the previous row, add one to it and set it as the value in the current post's id field. How do I do this?
Just set that field as primary key and auto-increment, it will automatically do this for you. You won't have to fetch the previous row and add that field value to next one.
The SQL query you need is:
SELECT max(id)
FROM tableName;
Set attribute auto increment for "ID" field in the table that contains 5 columns.
You can use sql query like
"INSERT INTO my_table (id auto_increment,primary key(id))";
then you can get...
and eachtime you need not worry to insert id ,it will automatically increments
I would not recomend doing this as it could lead to a race condition.
Change the table structure and set the id field to be the primary key and set it to auto increment. This way anytime a new row is added, it will auto-magically be assigned the next ID.
see this answer on details of how to set auto increment.
here is the query to alter your table and it will set your field or column as primary key and also auto increment it.
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;

MySQL Insert on Duplicate Key

I am building a rating system, and i want to insert a new row, if the name field does not already contain the name i want to insert, and if it does exist, i want to increase the count field by 1
For example, if i have a row the the name 'Tom' and i try to insert another row with the name 'Tom, then i want to +1 for the field count on the row that already exists.
If a row with the name 'Tom' does not exist, i want to insert a new one and set count to 1.
I know i could do this with about 3 SQL statements and some if statements, but that would slow down the script as 2/3 sql commands are being executed.
Any ideas?
Thanks!
see INSERT ... ON DUPLICATE KEY UPDATE
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed.
e.g.
INSERT INTO table (name,counter) VALUES ('Bob', 1)
ON DUPLICATE KEY UPDATE counter=counter+1

Categories