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.