I have 3 tables: A, with positions; B, with references; and C where I do the relations between positions and references. Each position can have more than one reference, but the opposite isn't allowed. Okay. My routine: When the page that saves the relations between positions and references is loaded by submit buttom from the form, I do this: 1. See if this position exists If yes, go to 2. If no, display an error. 2. See if this reference exists. If yes, add the relation between this reference and this position. If no, add this reference and after, add the relation between this reference and this position.
This is my basic routine. My problem? When the reference doesn't exists, the script add a new reference and after, the relation between the new reference and the position... But it's doesn't working well. At first time, the script only adds the reference, but don't add the relation... I need back in the page and add again to work.
My code:
<?php
#verify if this position exists at db
if(!empty($_POST['position'])) {
$query_position = "SELECT `PositionsId`, `PositionsIdentifier` from `positions` WHERE `PositionsId` = '".$_POST['position']."'";
$select_position = mysqli_query($connect, $query_ref);
#if yes, proceed to insert the new reference into this position...
if (mysqli_num_rows($select_position)) {
#verify if the reference exists at DB
if(!empty($_POST['ref'])) {
$query_ref = "SELECT `ReferencesId`, `ReferencesIdentifier` from `references` WHERE `ReferencesIdentifier` = '".$_POST['ref']."'";
$select_ref = mysqli_query($connect, $query_ref);
if (mysqli_num_rows($select_ref)) {
$list_ref = mysqli_fetch_array($select_ref, MYSQLI_ASSOC);
#verify if this reference exists to this position
$query_reference = "SELECT `PositionRefId`, `PositionsRef_PosId`, `PositionRef_RefId` FROM `positions_refs` WHERE `PositionsRef_PosId` = '".$id."' AND `PositionsRef_RefId` = '".$list_ref['ReferencesId']."'";
$select_reference = mysqli_query($connect, $query_reference);
if (mysqli_num_rows($select_reference)) {
echo "This reference exists to this position...";
}
else {
#insert this reference to this position
$query_insert_reference = "INSERT INTO `positions_refs` (`PositionsRef_PosId`, `PositionRef_RefId`) VALUES ('".$id."','".$list_ref['ReferencesId']."')";
if (mysqli_query($connect, $query_insert_reference)) {
echo "OK.";
}
else {
echo "Error.";
echo "<br /><br />";
echo mysqli_error($connect);
}
}
}
else {
#if don't exists at db, insert the new reference
if(!empty($_POST['ref'])) {
$query_insert_ref = "INSERT INTO `references`(`ReferencesIdentifier`) VALUES ('".$_POST['ref']."')";
if (mysqli_query($connect, $query_insert_ref)) {
#select the new reference ID
$query_new_ref = "SELECT `ReferencesId`, `ReferencesIdentifier` from `references` WHERE `ReferencesIdentifier` = '".$_POST['ref']." LIMIT 1'";
$select_new_ref = mysqli_query($connect, $query_new_ref);
if (mysqli_num_rows($select_new_ref)) {
$list_new_ref = mysqli_fetch_assoc($select_new_ref, MYSQLI_ASSOC);
#insert the new reference to this position
$query_insert_reference = "INSERT INTO `positions_ref`(`PositionsRef_PosId`, `PositionRef_RefId`) VALUES ('".$id."','".$list_new_ref['ReferencesId']."')";
if (mysqli_query($connect, $query_insert_reference)) {
echo "OK.";
}
else {
echo "Error.";
}
}
else {
}
}
else {
echo "Error.";
}
}
else {
echo "Error";
}
}
}
else {
echo "Error";
}
}
}
else {
echo "Error.";
}
?>
I don't know another way to do this that works correctly... Have any way to do this: add the new reference into DB and after, without reload page, etc, add this new reference into the position?
Thanks,