简体   繁体   English

如何将两个记录插入到不同的表中,首先使用insert,然后从insert php mysql中进行选择

[英]How to insert two records into different tables first one with insert and then with select from the insert php mysql

My question is not easy to explain for myself, but I will train. 我的问题不容易为自己解释,但我会进行培训。

I can insert a record into a table´s column form a form with php to mysql without problems. 我可以使用php到mysql的表格将记录插入表格的表格中,而不会出现问题。 This insert generates an auto_increment record in another column from the same table. 此插入将在同一表的另一列中生成一个auto_increment记录。

So I want to select this new auto_increment record and insert it in another table. 因此,我想选择此新的auto_increment记录并将其insert另一个表中。

<?php
            // process form`enter code here`
            $db_host="myhost";
            $db_user="myuser";
            $db_password="mypassword";
            $db_name="mydbname";
            $db_table_name="table1";
            $db_table_name2="table2";

            $record1= utf8_decode($_POST['record1']);
            $record2= utf8_decode($_POST['redord2']);

            $db_connection = mysqli_connect($db_host,$db_user,$db_password,$db_name);

            if (!$db_connection) {
                die('Could not connect to the database');
            }
            //insert record1 into table1
            $insert_value = 'INSERT INTO `' . $db_name . '`.`'.$db_table_name.'` (`name`) VALUES ("' . $record1 . '")';

            $result1 = $db_connection->query($insert_value);

            //consult auto_increment column from table1
            $select_value = 'SELECT column FROM table1 WHERE name = "' . $record1 . '"';

           //insert record2 into table2 in a row where a column is like $record1 
            $insert_value2 = 'INSERT INTO `' . $db_name . '`.`'.$db_table_name2.'` (`column1, column2`) VALUES ("' . $record2. '","' . $select_value  . '") WHERE name = "'.$db_table_name2.'"';

            $result2 = $db_connection->query($select_value);

            mysqli_close($db_connection);

        ?>
$query="Insert Statement";
$RunQuery = $db_connection->prepare($query);

If you're using PDO, it's PDO::lastInsertId(). 如果您使用的是PDO,则为PDO :: lastInsertId()。 So if your database handle is called $link: 因此,如果您的数据库句柄称为$ link:

$LastID = $db_connection->lastInsertId();

Or, If you're using MySQLi, it's mysqli::$insert_id or mysqli_insert_id(): 或者,如果您使用的是MySQLi,则为mysqli :: $ insert_id或mysqli_insert_id():

$LastID = $db_connection->insert_id;

Now, Use this $LastID in next Query. 现在,在下一个查询中使用此$ LastID。

For more info, check mysql_insert_id , mysql_query in PDO 有关更多信息,请在PDO中检查mysql_insert_idmysql_query

$insert_value2 = 'INSERT INTO `' . $db_name . '`.`'.$db_table_name2.'` (`column1, column2`) VALUES ("'.$record2.'","'.$LastID.'") WHERE name = "'.$db_table_name2.'"';

$result2 = $db_connection->query($insert_value2);

The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. mysqli_insert_id()函数返回由查询生成的ID,该查询是对具有具有AUTO_INCREMENT属性的列的表进行的。

If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero. 如果最后一个查询不是INSERT或UPDATE语句,或者修改后的表没有具有AUTO_INCREMENT属性的列,则此函数将返回零。

For example, 例如,

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
echo $mysqli->insert_id; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM