tags:

views:

89

answers:

7

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.

A: 

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.

nash
Did not work :(
tor
what did not work?
nash
that's the question I'm asking my self.
tor
+2  A: 

You're probably looking for an insert ... select statement.

Tobias Cohen
I think this is what I'm looking for but how do I select from one database and add it to another?
tor
I tried it from my previous question. http://stackoverflow.com/questions/1845254/php-mysql-table-problem
tor
A: 

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

lyrae
This does not work for some reason.
tor
I tried it from my previous question it failed. http://stackoverflow.com/questions/1845254/php-mysql-table-problem
tor
Are yo saying I'm going to have the call the database agian?
tor
A: 

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.

Platinum Azure
I'm trying to get this part of the question that was semi answered to work.http://stackoverflow.com/questions/1845254/php-mysql-table-problem
tor
A: 

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

nash
A: 

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.

Mailo
A: 

what is the structure of your database? The names of your tables, columns?

Py