How to insert the id that was generated from one table into another in PHP AND MYSQL?

-1

Hello community today I have a small problem that I have tried to solve but I do not get to the point, I read that this has nothing to do with the database but with the programming that I have in php and well, I have some tables relate example:

dficha- > idfy, idlib, idcarrera

mlibro- > idlibro, campo1, campo2

ccarrera- > idcarrera, campo1, campo2

The general table is the tab where the ids of the other two tables are stored, well and detailing more thoroughly I have a form in php where I have the fields of the book and the career, in this case I fill in the corresponding fields and the career field I have the default values career example: administration, engineering

So I have everything right, I send my form and the tables are filled, but here comes the problem that the id of the table mlibro is obvious autoincremental, but the id of the table dficha is not! and what I want is to send the id that was generated when entering in this case, a new record within the table mlibro I can do to send that id that was generated to the tab for your help Thanks!

    
asked by Kevin Duarte 29.04.2017 в 02:49
source

1 answer

-1

Look it's easy to get the id of what you just inserted I leave the php documentation page and the sample code, in the $mysqli->insert_id part with this you choose the id autoincremental, then it would only be a matter of doing another insert in your other table difcha .

This is what you put in the PHP documentation for insert_id :

  

The mysqli_insert_id() function returns the ID generated by a   query in a table with a column that has the attribute   AUTO_INCREMENT If the last query was not an INSERT statement or   UPDATE or if the modified table does not have a column with the attribute   AUTO_INCREMENT, this function will return zero.

Since you have not shared any code in your question, I'll give you the example that comes directly in the PHP documentation linked above. In this example, a temporary table is created that is used to perform an insertion, read the last inserted ID, and then delete the temporary table. In your code you probably do not need to create / delete a temporary table and it will suffice for you to do $mysqli->insert_id or mysqli_insert_id() (depending on whether you use the object-oriented or procedural method) after the INSERT.

  

Example # 1 Example of $ mysqli-> insert_id

     

Object-oriented style

<?php
$mysqli = new mysqli("localhost", "mi_usuario", "mi_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Error de conexión: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

printf ("Nuevo registro con el id %d.\n", $mysqli->insert_id);

/* drop table */
$mysqli->query("DROP TABLE myCity");

/* close connection */
$mysqli->close();
?>
    
answered by 29.04.2017 / 08:12
source