value auto-incremental ID

1

I have a question about the AUTOINCREMENT values in MySQL

In a system that I am developing allows you to save projects and the corresponding paperwork that must be kept (records, letters, forms), in the registration window it asks you to enter a code, which will be the ID, me in my project table I have the autoincremental value.

But what happens, the code I have to save the project reads the value that is in the proy_id text box, and then does the saving, if I left that empty box in the database if I kept the record as id: 1, id: 2, id: 3, but I did not save the paperwork because I could not find the project ID to save.

My question is how can I do so that an automatic value appears in the code, if possible the one that will be saved in the database, that comes out 1, 2, 3, 4, 5

    <input type="text" maxlength="5"  class="form-control" name="proy_id" id="proy_id" value="" placeholder="Código del Proyecto" >
    </div>
</div>

They told me to put a "value", but I do not know how to call that from the database or how to generate it

this code saves the project and its requirements

<?php 
include 'conexion.php'; 
bd_proyecto_agregar($_REQUEST);  
foreach ($_REQUEST['requ'] as $requisito_id){
  $requisito = array('proy_requ_id' => NULL, 'proy_id' => $_REQUEST['proy_id'], 'requ_id' => $requisito_id);
  bd_proyecto_requisito_agregar($requisito);
} 
header("Location: listado2.php"); 
exit; 
?>
    
asked by Victor Alejandro Alvarado Vilo 26.05.2016 в 06:56
source

4 answers

1

Your "bd_proyecto_agregar" function should return the project ID that was created automatically in mysql, to get this ID you should call the function " mysql_insert_id () " just after doing the INSERT.

Once you return this ID, you save it in a variable and pass it instead of passing "$ _REQUEST ['proy_id']".

With what the code would look like:

<?php 
include 'conexion.php'; 
$id = bd_proyecto_agregar($_REQUEST);  
foreach ($_REQUEST['requ'] as $requisito_id){
  $requisito = array('proy_requ_id' => NULL, 'proy_id' => $id, 'requ_id' => $requisito_id);
  bd_proyecto_requisito_agregar($requisito);
} 
header("Location: listado2.php"); 
exit; 
?>
    
answered by 26.05.2016 / 18:19
source
1

Good morning,

I think there is also a confusion of concepts here, in principle, the ID should not be modified, instead, there should be an ID field and one called CODE, I recommend making a modification to the database, that is: < br>
Define a field called ID that is auto-incremental and a field (in this case proy_id, I suppose) where the code of your project is saved, the idea of an AUTOINCREMENT field in MySQL is to have a key that is never modified, in this case, a unique identifier of your records that you have 100% security that will not be repeated in your table because it automatically increases, on the other hand, in your proy_id field that can be a varchar (for example), you could keep a different nomenclature, PROY- 001-A, PROY-001-B or something similar.

How to do record manipulation then?

Simple, when you do an INSERT, you write

  

INSERT INTO table_name (proy_id, description, etc ...) VALUES (   "PROY-001-A", "This project is about", etc ...);

And being an INSERT, the AUTOINCREMENT field will be automatically entered and it will be ready for the next record and when you do an UPDATE you write:

  

UPDATE table_name SET proy_id="PROY-001-B", description="this is another modification", etc ... WHERE id = $ id

Where $ id would be a value you are sending in your $ REQUEST (something like the value you were told to add) that could be seen as

<input type="hidden" name="id" value="<?php echo $respuestaBD['id']?>" />

And what would you say to register to update, and how to send / call the ID? you could call the id as $id = $_REQUEST['id'] and send it in your array $ requirement defined in the code you published.

I hope this approach is useful to you.

Greetings,

    
answered by 26.05.2016 в 18:41
0

I think that simply when you do the insert of the project in the database, you must omit the field of your ID, then MySQL to see that there is no value for that auto record will assign the corresponding value auto increment of the column.

I hope you help, greetings.

    
answered by 26.05.2016 в 17:27
0

If you want the id to be assigned by the user (for example, because it is printed on a physical form), you should not use auto increment. For the associated documents in the other table you should have two columns that form your index an id (of the document) auto inc and a project id then if you enter the project 993 in your project table it will be:

id: 99
obs: proyecto ingresado por pepito


en la de documentos:

id: 1023
proyecto_id: 99
documento: planilla excel

id: 1024
proyecto_id: 99
documento: documento pdf

and an index for (project_id, id) in the second table would help you to access the project documents in order and quickly

SELECT * from documentos where proyecto_id=99 order by id
    
answered by 27.05.2016 в 07:01