数据已在数据库中更新,但有错误

时间:2019-12-09 05:50:50

标签: php

在我的系统中,我具有更新数据的功能。单击“更新”按钮后,数据将在数据库中更新,但它还会显示如下错误:

  

PHP注意:未定义的索引:中的badgeid   C:\ inetpub \ wwwroot \ tgotworker \ pages \ manage_engineer_admin \ edit_engineer_admin.php   在第33行

     

PHP注意:未定义的变量:中的全名   C:\ inetpub \ wwwroot \ tgotworker \ pages \ manage_engineer_admin \ edit_engineer_admin.php   在第210行

     

PHP注意:未定义的变量:   C:\ inetpub \ wwwroot \ tgotworker \ pages \ manage_engineer_admin \ edit_engineer_admin.php   在第220行

     

PHP注意:未定义的变量:   C:\ inetpub \ wwwroot \ tgotworker \ pages \ manage_engineer_admin \ edit_engineer_admin.php   在第221行

     

PHP注意:未定义变量:team_id   C:\ inetpub \ wwwroot \ tgotworker \ pages \ manage_engineer_admin \ edit_engineer_admin.php   在第231行

     

PHP注意:未定义变量:team_id   C:\ inetpub \ wwwroot \ tgotworker \ pages \ manage_engineer_admin \ edit_engineer_admin.php   在第231行

下面是我的代码:

<?php
$sql = "";
require_once "../../config/configPDO.php";
require_once "../../config/check.php";

if(isset($_POST['update']))
{   
  $badgeid = $_POST['badgeid'];

  // checking empty fields
  if(empty($badgeid)) { 

    if(empty($badgeid)) {
      echo "<font color='red'>Fac_Name field is empty.</font><br/>";
    }

  } else {  
    //updating the badgeid
    $sql = "UPDATE users SET roles_id = :roles_id, team_id = :team_id WHERE badgeid = :badgeid";
    $query = $conn->prepare($sql);

    $query->bindParam(':roles_id', $_POST['roles_id']);
    $query->bindParam(':team_id', $_POST['team_id']);
    $query->bindParam(':badgeid', $_POST['badgeid']);
    $query->execute();

    //redirectig to the display page. In our case, it is index.php
    header("Location: ../dashboard/dashboard_super_admin.php");
  }
}

//getting id from url
$badgeid = $_GET['badgeid']; //line 33

//selecting data associated with this particular id
$sql = "SELECT * FROM users LEFT JOIN roles on users.roles_id = roles.roles_id WHERE badgeid = :badgeid";
$query = $conn->prepare($sql);
$query->execute(array(':badgeid' => $badgeid));
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row)
{
  $badgeid = $row["badgeid"];;
  $fullname = $row["fullname"];
  $roles_id = $row["roles_id"];
  $team_id = $row['team_id'];
  $roles_name = $row["roles_name"];
}

$smt = $conn->prepare("SELECT * FROM team");
$smt->execute();
$data = $smt->fetchAll();

?>

   <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
  <table class = "table table-bordered">
    <tr>
      <td width="20%"><b>Full Name</b></td>
      <td width="80%"><?php echo $fullname; ?></td> //line 210
    </tr>
    <tr>
      <td width="20%"><b>Badge ID</b></td>
      <td width="80%"><?php echo $badgeid; ?></td>
    </tr>
    <tr>
      <td width="20%"><b>Role</b></td>
      <td width="80%">
        <select class="form-control" name="roles_id">
          <option value="1" <?php echo $roles_id == '1'? 'selected': '';?> >Super Admin</option> //line 220
          <option value="2" <?php echo $roles_id == '2'? 'selected': '';?> >Engineer</option> //line 221
        </select>
        <input type="hidden" name="badgeid" value="<?php echo $badgeid ?>">
      </td>
    </tr>
    <tr>
      <td width="20%"><b>Team</b></td>
      <td width="80%">
      <select class="form-control" name="team_id">
      <?php foreach ($data as $row): ?>
        <option value="<?php echo $row["team_id"]; ?>" <?php echo $row["team_id"] == $team_id ? 'selected': ''; ?>><?php echo $row["team_name"]; ?></option> //line 231
      <?php endforeach ?>
      </select>
      <input type="hidden" name="badgeid" value="<?php echo $badgeid ?>">
      </td>
    </tr>  
  </table><br>
  <div align="center">
    <td><button class ="btn btn-primary btn-block" name="update" value="Update" onclick="update()">Update</button></td>
  </div>
</form>

1 个答案:

答案 0 :(得分:1)

问题是您将$_SERVER["PHP_SELF"]用作表单操作。

这将从重定向页面中删除$_GET变量,这意味着您将被重定向到例如index.php而不是index.php?badgeid=1

因此,此行会引发通知$badgeid = $_GET['badgeid']; 并且使查询没有结果,因此其余变量永远不会初始化,从而导致您得到其余通知。

为了解决这个问题,您可以使用$_SERVER['REQUEST_URI']代替,它将保留变量或保存并通过其他方式传递badgeid