Stackoverflow, I write this question because I have a system to register projects and their corresponding forms.
I have:
a registration form
a php code (contained in the connection.php file as a function) that is responsible for saving the project
a php code that saves each template or requirement using the project ID as a guide, this is stored in a nested table containing project_id and requirement_id.
The problem is that the code to save the spreadsheets in the nested table.
When you click on Save the project in the registration form the guard based on the project code that will be generated in the Project ID field.
1) I would like that value to be auto incremental, but based on the last ID that exists in the database, for the order of 1 2 3 4, how could I do it, since I must write each value, they told me to use the Mysql_LastinsertId, something like that, and I think I already have it in the code, but I do not know how to pass that value to the registration form.
2) currently the system generates the auto-incremental ID, but saves that ID with an empty project every time the form is loaded, therefore ID 6 appears, and once the database is filled with a project without data with ID 6, making it impossible to save that project because it already exists.
3) how would that value be placed on the form in the valued field?
4) depending on the function of obtaining the last insert I have it in the function of adding
Thank you in advance for all the help
Codes:
1) Function to add the projects to the database (included in conexion.php)
function bd_proyecto_agregar($d){
$sql = sprintf("INSERT INTO proyecto(proy_id, plan_proy, proy_deno,, objg_proy, obje_proy, fech_insc, nomb_comu, resp_comu, telf_inst, telf_resp, parr_id, esta_proy_id, deta,obsv_proy)
VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
$d['proy_id'],
$d['plan_proy'],
$d['proy_deno'],
$d['objg_proy'],
$d['obje_proy'],
$d['fech_insc'],
$d['nomb_comu'],
$d['resp_comu'],
$d['telf_inst'],
$d['telf_resp'],
$d['parr_id'],
$d['esta_proy_id'],
$d['deta'],
$d['obsv_proy']);
$res = sql($sql);
$id = sql2value("SELECT LAST_INSERT_ID()");
return $id;
}
2) The function of adding the requirements in the nested table when saving the project (proc_inscripcion2.php):
<?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;
?>
3) Code included in the POST of the registration form:
<form id="frmAgregar" class="form-horizontal" method="POST" action="proc_inscripcion2.php" role="form">
4) Code to show the value of the auto-incremental ID and function that is supposed to bring the ID of the function:
$valor= bd_proyecto_agregar($id);
<input type="text" maxlength="5" class="form-control" name="proy_id" id="proy_id" value="<?=$valor?>" placeholder="Código del Proyecto" >