Two insert query... in the same click - php

I have two text fields and a button, on the click i need to save them into MySQL Tables.
The top text box field data in table 1 and data in text box in table two...
How to perform it.

Well, i guess your form execute some server-side PHP.
Then in this PHP juste use mysql_query() 2 times, with your 2 insert.

As MySQL does not seem to support multiple inserts in otwo tables you can do it with a transaction with two inserts or with a stored procedure.

Related

Inserting dynamically generated form data into mysql

I have a simple HTML form with three fields name,school and city. Also within the form, I have a button which I am using to insert a sub form with values neighbourhood and population. The crud mechanism is using jquery append.
In my database, I am saving the main form data in one table and the sub form data in another table.
My question is, during the creation of data in the main form, if I insert the data of the main form into one table and get the last inserted Id and use the id as a reference when inserting data into the second table, will this approach always work in both mysql and ms SQL?
If not is there a better approach I can adopt?
It will work, as long as your dynamically created input has the proper name attribute..
For example: name="txt_neighbourhood"

How to use insert, update, delete query in sql at same time?

Just stuck up, how to use multiple query in single form to execute different query.
eg:
Like in image, Table 1 has records.
what i am doing is:
extract first 3 record from table 1 and show in a form (frontend) and when this process is gone..
i need at same time , this happens without refresh and without user data or any frontend disturbance.
Once this data view to user:
i need:
same time, those 3 records get copy to table 2 from table 1. like Roll_no and Name.
Then, remove those 3 records from table 1.
i am doing this to prevent duplicate data and something.
so any code will be helpful.
Your question is quite unclear, but it seems you are concerned about data integrity performing multiple queries. What you need is to use transaction to group a set of commands toghether. This way either all commands will succeed or your database will not be affected by any of them.

How to export part of a table as SQL in Adminer?

In Adminer, I can export whole tables with the "Export" menu option. When I enter a query, I can export the results as .CSV or .TSV, but not as SQL.
Is there any option to do so? A plugin or a config switch?
Adminer doesn't offer SQL export of a general SQL query because the query may join more tables which would be impossible to export to SQL.
To export a subset of rows in a single table, go to the Select view of this table, filter the results as you wish and check the rows you want to export (or check 'whole result' to export all rows in the result). Then use the Export button under the table.
In current version it is possible with Search
Select column
Select SQL as mark
Into value field fill subquery IN (SELECT id FROM ...)
if you click on export then on that page see there is a radio button named dump. put no of rows you wanted to pick up and starting record index number. then you will get your expected result
Here is how I did this:
Go to Adminer-> Click on Dump in top left side.
Output-> check save radio button
Format-> check SQL radio button
Go to below Export button
Mark checkboxes of Tables (if you want to data then mark checked Data)
If you want 1 table then mark one checkbox of a table.
Click on Export button.
Here we go :), Coool ;)

Manipulate a text box and displaying it dynamically?

I want to create a dynamic number display of texbox, there will be a a textbox and a submit button, then the numeric value of my first textbox shall display also the number of textbox. Which will allow me to input my records in my database. I am using MySQL for my database and how would I insert the records to my table if the first set records of my textbox is not the same as the second (example I input 3 on my first textbox then three textbox shall appear and a submit button, on my second set of records I decided to have 6 on the first textbox then six textbox shall appear in the page) my question again how am I going to do this?
If ever I use array in fetching records and indexing the value of my dynamic textbox, what type of array shall I use to this? How will I going to name the value of my textbox dynamically? and lastly the I wanted the restrictions also that will set the maximum number of textbox.
Ok. Multiple textboxes, let's call them columns. The first record the user enters in each row is the number of columns they require. The submit button would just call a javascript function to create the required amount of columns for that row on the page. This would have the restriction as to how many can be created and the PHP would also ignore extras if the user works out how to cheat it.
Jumping to your last question (because the second question needs this info). You have decided what the maximum number of columns is so the easiest way if that number isn't large would just be to create a table that can satisfy that many columns. Some fields would be NULL on some rows.
It doesn't matter that the number of columns is different when you want to insert because, for each row, you expect up to a certain amount and only deal with the values you are given, those missing are NULL. When you come to output them you only output those that aren't NULL.
The bit about using an array in fetching records and indexing doesn't make sense to me sorry, but I suspect you're fishing for what php database class to use get the data back from the database? I think the trend is mysqli. Personally I use a bespoke one.

PHP/MySQL how can I select a value from a MySQL database?

I was wondering how can I select a value from a database that a user just entered it into and then add it to another mysql table all in the same script before the script is finished running.
You're probably looking for an insert ... select statement.
If you're talking about adding a value that a user just entered into a form, to something, and then putting that into the database, you should do the addition while in PHP. There's no point in going to the database after you've just inserted the value for this purpose.
If I'm misunderstanding something, please elaborate your question and let us know WHY you would want to figure out a just-inserted database value and do an operation on it, rather than trying to do it before you insert in the first place.
Also, if it's a fairly simple modification consider using an UPDATE statement, not a select --> insert.
Like nash said, you perform a select.
But to get the data from the row that the user just entered, you'll need:
mysql_insert_id()
Which grabs the last ID inserted (this is assuming you have an increment id column)
So assuming just entered his first and last name in a form, you'd insert his first and last name in the database(which i assume you know how since the title of this question is "SELECT a value from MySQL database"), you can get what he just entered by:
$last_id = mysql_insert_id();
If there are no rows on that table yet, then this will return 1. $last_id is now 1 (one).
To select:
SELECT * FROM users WHERE userID = "$last_id"
this will grab what the user just inserted....however, this seems pointless as you can use the variables from the form he just filled
enter code here
In the PHP MySQL module, you normally perform a mysql_select_db() to switch database.
You can insert your data into tables in different databases by switching between them with that function.
However, you can insert data into any table of any database (which the user has access to) by prefixing the database name to the table like so:
INSERT INTO test_db.test_table (`column1`,`column2`) VALUES ('abc',123);
You can use that also to insert data from one table into another using:
INSERT INTO `db1`.`myTable` (`column1`,`column2`) SELECT `column1`,`column2` FROM `db2`.`myTable` WHERE `id`= 5
The WHERE id part should obviously match the id of a row in db2.myTable
If you use doctrine you have the inserted data in the object representing the table and in addition you have primary key assigned for the record inside the object.
Con is doctrine is huge database abstraction layer, so if your application is not big doctrine is hammer for mosquito.
what is the structure of your database? The names of your tables, columns?
Some tutorial that you may want to look at: (grabbed from google)
http://www.phpf1.com/tutorial/php-mysql-tutorial.html
In theory you perform a select, take the data you need and perform an insert.

Categories