如何在更新表单的if(isset)语句中使用PHP获取URL参数

I know you can get the URL parameters by using if(isset($_GET['id'])){ $id = $_GET['id']; and this works great, if I then close that 'if' statement and create a new one for the update/append-entry form on this same page (its the display page for each ind. database entry), I can't get the id that's been passed to the URL in this next if statement.

It is the if-statement that submits a new row ("condition") to my child table ("conditions") that has a foreign key that connects it to the parent table of health providers (the display page gets the provider's id from the search engine selection and displays their information, then has this "append entry" form at the bottom if someone wants to add a new health condition treated by this doctor.)

Everything works if I give it the right id number directly in my PHP ($id = 56), but not if I try to grab it from the URL INSIDE this second if statement ($id = $_GET['id'];).

if (isset($_GET['providerid']))
{
$providerid = $_GET['providerid'];  /*THIS WORKS GREAT*/

$sql = "SELECT *, GROUP_CONCAT(DISTINCT conditions.condition_name 
SEPARATOR ', ') AS all_conditions FROM `providers` INNER JOIN 
`conditions` ON `providers`.`id` = `conditions`.`prov_id` WHERE 
`prov_id`= $providerid";

$data = mysqli_query($connection, $sql) or die('error');

if(mysqli_num_rows($data) > 0){
  $numresults = mysqli_num_rows($data);

    while($row = mysqli_fetch_assoc($data)){
    $providerid = $row['id'];
    $providerfirstname = $row['provider_first_name'];
    /*etc....*/
    $conditions = $row['all_conditions'];

/*table displays provider info:*/
     echo "<br><h1>".$providerfirstname." ".$providerlastname." 
 </h1>";
     echo '<TABLE id="myTable" width="350px" border="1">';
           //etc....(cutting out details)
     echo '<tr><td><div id="myTable"><a href="#" id="addNew">Add+ . 
  </a></div></td><td><b>Conditions Treated:</b></td> . 
 <td>'.$conditions.'</td></tr>';

/*If user wants to add a new condition treated by provider that's 
not listed:*/
     echo '<tr><td></td><td>Add a new condition:</td><td><form 
action="profilebackup.php" method="POST"><input type="text" 
size="40" name="newcond" value="" placeholder="Add a Condition" /> . 
<input type="submit" name="add1" value="Add"><br></td></tr>';

            }
     echo '</TABLE>';

    }
   else {
        echo "0 results";
   }
}

/*sends added conditions to child table, EVERYTHING WORKS EXCEPT 
GETTING ID (WHICH WORKED ABOVE):*/

if(isset($_POST['add1'])){
  $providerid = $_GET['providerid'];
  $condition = mysqli_real_escape_string($connection, 
$_POST['condition']);


$insql = "INSERT INTO `conditions` (condition_name, prov_id) VALUES 
('$condition','$providerid')";

    if(mysqli_query($connection, $insql)){
      echo "Thank you! Your provider has successfully been submitted 
      to the database!";
     }
     else {
       echo "Sorry, there was an problem submitting your provider to 
       the database." . $insql . mysqli_error($connection);
      }
    }

Everything works EXCEPT the GET function in the second if-statement. It returns error code that it does not have the correct foreign key constraint: "Cannot add or update a child row: a foreign key constraint fails" because it's not grabbing the id.

You are submitting a form via post to profilebackup.php but the handler for this form is trying to get the providerid. You need to send the providerid as a query string in the form's action if you want to be able to access it via $_GET.

<form method="post" action="profilebackup.php?providerid=<?php echo $providerid; ?>" ...

The proper way to do this would be to include a hidden input in the form and then access it via $_POST consistently.

<form method="post" action="profilebackup.php">
    <input type="hidden" name="providerid" value="<?php echo $providerid; ?>">
    ...
</form>

And the handler code.

<?php
...

// Notice that now you can use $_POST consistently.
if (isset($_POST['add1'])) {
    $providerid = $_POST['providerid'];
    ...

The problem lines in $sql statement where it take variable $providerid as string, not a php variable. $_GET works fine even if sending a different protocals such as POST, PUT, DELETE,... Something like this should work as expected:

$sql = "SELECT *, GROUP_CONCAT(DISTINCT conditions.condition_name 
SEPARATOR ', ') AS all_conditions FROM `providers` INNER JOIN 
`conditions` ON `providers`.`id` = `conditions`.`prov_id` WHERE 
`prov_id`= " . $providerid ;