如何在更新表单的 if(isset) 语句中使用 PHP 获取 URL 参数
How to GET URL parameters with PHP inside if(isset) statement for update form
我知道您可以使用 if(isset($_GET['id'])){
$id = $_GET['id'];这很好用,如果我然后关闭 'if' 语句并在同一页面(它是每个 ind. 数据库条目的显示页面)上为 update/append-entry 表单创建一个新语句,我不能在下一个 if 语句中获取已传递给 URL 的 ID。
向我的 child table ("conditions") 提交新行 ("condition") 的 if-statement 有一个外键将其连接到 parent table 医疗保健提供者(显示页面从搜索引擎选择中获取提供者的 ID 并显示他们的信息,然后如果有人需要,则在底部显示此 "append entry" 表单添加由该医生治疗的新健康状况。)
如果我直接在 PHP ($id = 56) 中给它正确的 ID 号,一切正常,但如果我尝试从第二个 if 语句中的 URL 中获取它,则一切正常($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);
}
}
除第二个 if-statement 中的 GET 函数外,一切正常。它 returns 错误代码表明它没有正确的外键约束:"Cannot add or update a child row: a foreign key constraint fails" 因为它没有获取 ID。
您正在通过 post
向 profilebackup.php 提交表单,但此表单的处理程序正在尝试 get
供应商编号。如果您希望能够通过 $_GET
访问它,您需要在表单的操作中将 providerid 作为查询字符串发送。
<form method="post" action="profilebackup.php?providerid=<?php echo $providerid; ?>" ...
正确的做法是在表单中包含一个隐藏输入,然后通过$_POST
一致地访问它。
<form method="post" action="profilebackup.php">
<input type="hidden" name="providerid" value="<?php echo $providerid; ?>">
...
</form>
和处理程序代码。
<?php
...
// Notice that now you can use $_POST consistently.
if (isset($_POST['add1'])) {
$providerid = $_POST['providerid'];
...
$sql
语句中的问题行将变量 $providerid
作为字符串,而不是 php 变量。即使发送 POST、PUT、DELETE 等不同的协议,$_GET 也能正常工作......像这样的东西应该按预期工作:
$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 ;
我知道您可以使用 if(isset($_GET['id'])){ $id = $_GET['id'];这很好用,如果我然后关闭 'if' 语句并在同一页面(它是每个 ind. 数据库条目的显示页面)上为 update/append-entry 表单创建一个新语句,我不能在下一个 if 语句中获取已传递给 URL 的 ID。
向我的 child table ("conditions") 提交新行 ("condition") 的 if-statement 有一个外键将其连接到 parent table 医疗保健提供者(显示页面从搜索引擎选择中获取提供者的 ID 并显示他们的信息,然后如果有人需要,则在底部显示此 "append entry" 表单添加由该医生治疗的新健康状况。)
如果我直接在 PHP ($id = 56) 中给它正确的 ID 号,一切正常,但如果我尝试从第二个 if 语句中的 URL 中获取它,则一切正常($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);
}
}
除第二个 if-statement 中的 GET 函数外,一切正常。它 returns 错误代码表明它没有正确的外键约束:"Cannot add or update a child row: a foreign key constraint fails" 因为它没有获取 ID。
您正在通过 post
向 profilebackup.php 提交表单,但此表单的处理程序正在尝试 get
供应商编号。如果您希望能够通过 $_GET
访问它,您需要在表单的操作中将 providerid 作为查询字符串发送。
<form method="post" action="profilebackup.php?providerid=<?php echo $providerid; ?>" ...
正确的做法是在表单中包含一个隐藏输入,然后通过$_POST
一致地访问它。
<form method="post" action="profilebackup.php">
<input type="hidden" name="providerid" value="<?php echo $providerid; ?>">
...
</form>
和处理程序代码。
<?php
...
// Notice that now you can use $_POST consistently.
if (isset($_POST['add1'])) {
$providerid = $_POST['providerid'];
...
$sql
语句中的问题行将变量 $providerid
作为字符串,而不是 php 变量。即使发送 POST、PUT、DELETE 等不同的协议,$_GET 也能正常工作......像这样的东西应该按预期工作:
$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 ;