dongyu1979 2019-07-06 02:48
浏览 108
已采纳

如何在更新表单的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.

  • 写回答

2条回答 默认 最新

  • doubo6658 2019-07-06 03:03
    关注

    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'];
        ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘