更新使用GET URL ID创建的页面上的MySQL记录

时间:2014-05-30 15:30:24

标签: php mysql url select get

我正在创建一个简单的报告系统,我希望从数据库中生成菜单和页面。

我在YouTube上看到了这个视频,并设法创建了一个包含以下代码的菜单。

我有一个名为Reports和列的数据库表,名为rep_id,rep_date,rep_ledit_date,rep_by,department,position,report和rep_to。另一个名为users的表,其名称为id,username,password,first_name,last_name,department,postion和passphrase。

我设法为报告表选择了一条添加的记录,但是,我有以下问题。 1. rep_to没有预先选择已经选择的选项 2.无法使用php更新记录注意:未定义的索引:第232行的C:\ wamp \ www \ cme \ edit-this-report.php中的rep_id,数据库没有更新。此行是选择报告表的位置 请参阅下面的php代码。

<?php
  if(isset($_SESSION['users'])) { 
      $uname = $_SESSION['users']; 
      $fname = $_SESSION['firstname'];
      $lname = $_SESSION['lastname'];
      $dep = $_SESSION['depart'];
      $pos = $_SESSION['position'];
      $query = mysqli_query($con, "SELECT * FROM users WHERE username = $uname");
      while($row = mysqli_fetch_assoc($query)) {
          $id=$row['id'];
          $fname=$row['first_name']; 
          $lname=$row['last_name'];
          $dep = $row['department']; 
          $pos = $row['position']; 
          $repby = $row['first_name'] . " " . $row['last_name']; 
          $repdep = $row['department'];
          $reppos = $row['position'];
      } 
   } 

        mysqli_select_db($con, $db_name);
        $edit= "SELECT * FROM reports WHERE rep_id = '{$_GET['rep_id']}'";
        $result = mysqli_query($con, $edit) or die(mysqli_error($con));

        $row2 =  mysqli_fetch_array($result);

  if(isset($_POST['update'])) {
      $_GET['rep_id']=$row2['rep_id'];
      $reptype = $_POST['reporttype'];
      $report = $_POST['report'];
      $repto = $_POST['reportedto'];
      $update=(mysqli_select_db($con, $db_name));
      if(!$update) {
          die('Could not connect: ' . mysql_error($con));
      }
      else {
          $sql = "UPDATE reports SET rep_type='$reptype', report='$report', rep_to='$repto',
                  rep_ledit_date=NOW() WHERE rep_id='{$_GET['rep_id']}'";
          $retval = mysqli_query($con, $sql);
          if(!$retval ) {
              $errorMessage='Could not update data: ' . mysqli_error($con);
          }
          else {
              $success="Updated data successfully\n";
              header("location:edit-this-report.php"); 
              mysqli_close($con);
          }
      }
  }

?>

表格代码:

  <form name="editor" action="edit-this-report.php" method="post" >
   <p class="inline">
    <span>
       <label for="mem">Reported by</label>
  <input type="text" name="reportedby" maxlength="20" disabled value="<?php print  $fname . " " . $lname; ?>" />
    </span>
   </p>
   <p class="inline">
     <span>
        <label for="mem">Department Name</label><input type="text" name="repdepart" disabled size="100" maxlength="100" value="<?php print $dep; ?>">
     </span>
   </p>

   <p class="inline">
      <span>
        <label for="mem">Position</label><input disabled type="text" name="repposition" size="100" value="<?php print $pos; ?>">
      </span>
   </p>

   <p>
     <span>
      <label for="mem">Report Type</label> 
       <select name="reporttype">
          <option value=""<?php if ($row2['rep_type'] === 'Daily Report') echo ' selected="selected"'; ?>>Daily Report</option>
          <option  value=""<?php if ($row2['rep_type'] === 'Weekly Report') echo ' selected="selected"'; ?>>Weekly Report</option>
          <option value=""<?php if ($row2['rep_type'] === 'Monthly Report') echo ' selected="selected"'; ?>>Monthly Report</option>
          <option value=""<?php if ($row2['rep_type'] === 'Quarterly Report') echo ' selected="selected"'; ?>>Quarterly Report</option>
          <option value=""<?php if ($row2['rep_type'] === 'Annual Report') echo ' selected="selected"'; ?>>Annual Report</option>
          <option  value=""<?php if ($row2['rep_type'] === 'Terminal Report') echo ' selected="selected"'; ?>>Terminal Report</option>
      </select>
     <span>
   </p>

   <p>
    <span>
         <label for="mem">Report</label> 
         <textarea name="report" id="report" rows="23" cols="auto" ><?php echo $row2['report'];?></textarea>
    <span>
   </p>
   <p>
    <span>
        <label for="mem">Reported to</label> 
        <select name="reportedto">
  <?php
  require ("includes/db.php");
        $q2= "SELECT * FROM users WHERE department like '%$repdep%'"; 
        $result3=mysqli_query($con, $q2) or die(mysqli_error($con));
        while ($getuser=mysqli_fetch_array($result3)){
        $repto=$getuser['first_name'] . " " . $getuser['last_name'];
  ?>
          <option value="<?php echo $repto; ?>"><?php echo $repto; ?></option>; 
        <?php       
        }       
        ?>

        </select>

    </span>
   </p>
    <span>
        <input name="update" type="submit" class="btn btn-large btn-primary" id="report_button" value="Submit Report" > 
        <input name="cancel" type="reset" class="btn btn-large btn-secondary" id="report_button" value="Cancel All Changes" >
    </span>
   </p>
  </form>

请帮我解决这个问题。

谢谢!

3 个答案:

答案 0 :(得分:1)

你必须确切地确定上面哪一行是第232行,而是&#34;未定义的索引&#34; PHP中的错误意味着您有一个数组($ _GET和$ _POST都是数组)并且您正在尝试访问一个值(在这种情况下,一个:$ _GET [&#39; rep_id&#39;] = 1;但它无法找到&#39;指数&#39;在数组中(在这种情况下&#39; rep_id&#39;)。

您正在通过尚未在页面上定义的索引访问数组元素。

编辑: 可能在这里:     $ edit =&#34; SELECT * FROM报告WHERE rep_id =&#39; {$ _ GET [&#39; rep_id&#39;]}&#39;&#34;;

$ _ GET引用了一个名为rep_id的url变量,但是你没有使用post来发送rep_id吗?在这种情况下尝试改变     $ _GET [&#39; rep_id&#39;] 至     $ _POST [&#39; rep_id&#39;]

答案 1 :(得分:1)

我没有在您的表单中看到rep_id变量集,因此在提交表单时您没有收到该值。您在前一个查询中依赖$ _GET ['rep_id']为您的UPDATE提供rep_id,但我看到您的表单中没有提供$ _GET变量。 (也许不是混合POST和GET的最佳实践,更好的是添加隐藏的表单var并将其设置为rep_id,并将其捕获为POST变量。)

尽管如此,我能想到使代码工作的最简单方法是将rep_id附加到表单操作属性:

<form name="editor" action="edit-this-report.php?rep_id=<?php echo $_GET['rep_id']; ?>" method="post" >

如果再有错误,请运行该报告并报告。

另一个问题,虽然可能不是show-stopper,但在SELECT * FROM users查询之前,$ uname var未被引用。但是,更大的未来问题是在SQL语句中使用不安全,用户提供或黑客可操作的变量会使数据库对SQL注入攻击开放。

更新:

考虑这样的事情:

<?php

session_start();

require ('includes/db.php'); // provides $con, select database

// get logon info
if ( !isset($_SESSION['username']) ) { header('Location: logon.php'); } // logon and set session vars
else {
  list($uname, $repby, $dep, $pos) = 
      array($_SESSION['username'], $_SESSION['repby'], $_SESSION['department'], $_SESSION['position']);
}

// get report id if GET'd
if ( isset($_GET['rep_id'])  ) {
    $rep_id = $_GET['rep_id'];
}

// update report if POST'd
else if ( isset($_POST['update'] ) ) {
    $rep_id  = $_POST['rep_id'];
    $reptype = $_POST['reporttype'];
    $report  = $_POST['report'];
    $repto   = $_POST['reportedto'];

    if ( mysqli_stmt_prepare($stmt, 'UPDATE reports SET rep_type= ?, report= ?, rep_to= ?, rep_ledit_date= ? WHERE rep_id= ?') ) {
        mysqli_stmt_bind_param($stmt, 'sssi', $reptype, $report, $repto, NOW(), $rep_id);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_close($stmt);
    }
    else { $errorMessage='Could not update report data: ' . mysqli_error($con); }
}
else { die('no report id'); }

// get/verify report info (can be moved to get'd if to save a db call when post'd)
list($rep_type, $report) = array('', '');
$stmt = mysqli_stmt_init($con);
if ( mysqli_stmt_prepare($stmt, 'SELECT rep_id, rep_type, report FROM Reports WHERE rep_id = ?') ) {
    mysqli_stmt_bind_param($stmt, 'i', $rep_id);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $rep_id, $rep_type, $report);
    mysqli_stmt_fetch($stmt);
    mysqli_stmt_close($stmt);
}
else { $errorMessage='Could not select report data: ' . mysqli_error($con); }

?>


 <form name="editor" action="edit-this-report.php" method="post" >
   <input type="hidden" name="rep_id" value="<?=$rep_id?>">
   <p class="inline">
    <span>
       <label for="mem">Reported by</label>
  <input type="text" name="reportedby" maxlength="20" disabled value="<?=$repby?>" />
    </span>
   </p>
   <p class="inline">
     <span>
        <label for="mem">Department Name</label><input type="text" name="repdepart" disabled size="100" maxlength="100" value="<?=$dep?>">
     </span>
   </p>

   <p class="inline">
      <span>
        <label for="mem">Position</label><input disabled type="text" name="repposition" size="100" value="<?=$pos?>">
      </span>
   </p>

   <p>
     <span>
      <label for="mem">Report Type</label> 
       <select name="reporttype">

<?php

list($rep_type_da, $rep_type_we, $rep_type_mo, $rep_type_qu, $rep_type_an, $rep_type_te) = array('', '', '', '', '', '');
switch ( $rep_type ) {
    case 'Daily Report': $rep_type_da = ' selected'; break;
    case 'Daily Report': $rep_type_we = ' selected'; break;
    case 'Daily Report': $rep_type_mo = ' selected'; break;
    case 'Daily Report': $rep_type_qu = ' selected'; break;
    case 'Daily Report': $rep_type_an = ' selected'; break;
    case 'Daily Report': $rep_type_te = ' selected'; break;
}

?>
          <option value="Daily Report"    <?=$rep_type_da?>>Daily Report</option>
          <option value="Weekly Report"   <?=$rep_type_we?>>Weekly Report</option>
          <option value="Monthly Report"  <?=$rep_type_mo?>>Monthly Report</option>
          <option value="Quarterly Report"<?=$rep_type_qu?>>Quarterly Report</option>
          <option value="Annual Report"   <?=$rep_type_an?>>Annual Report</option>
          <option value="Terminal Report" <?=$rep_type_te?>>Terminal Report</option>
      </select>
     <span>
   </p>

   <p>
    <span>
         <label for="mem">Report</label> 
         <textarea name="report" id="report" rows="23" cols="auto" ><?=$report?></textarea>
    <span>
   </p>
   <p>
    <span>
        <label for="mem">Reported to</label> 
        <select name="reportedto">
          <option value=""></option>
<?php

if ( mysqli_stmt_prepare($stmt, 'SELECT CONCAT(first_name, last_name) AS repto FROM users WHERE department LIKE ?') ) {
    mysqli_stmt_bind_param($stmt, 's', "%$dep%");
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $repto);
    while ( mysqli_stmt_fetch($stmt) ) {
        echo '<option value="' . $repto . '">' . $repto . "</option>\n";
    }
    mysqli_stmt_close($stmt);
}
else { $errorMessage='Could not select dep user data: ' . mysqli_error($con); }

?>

        </select>

    </span>
   </p>
    <span>
        <input name="update" type="submit" class="btn btn-large btn-primary" id="report_button" value="Submit Report" > 
        <input name="cancel" type="reset" class="btn btn-large btn-secondary" id="report_button" value="Cancel All Changes" >
    </span>
   </p>
  </form>

我没有设置数据库,所以我没有测试过它。如果您运行此操作并获得错误,请将其发布在评论中。

答案 2 :(得分:0)

除了bloodyKnuckles给出的解决方案外,我还得到了朋友的帮助。 为if(isset($_POST['update']))以下的帖子值提供初始值时也存在问题。

  <?php
  if(isset($_SESSION['users'])) { 
      $uname = $_SESSION['users']; 
      $fname = $_SESSION['firstname'];
      $lname = $_SESSION['lastname'];
      $dep = $_SESSION['depart'];
      $pos = $_SESSION['position'];
      $query = mysqli_query($con, "SELECT * FROM users WHERE username = $uname");
      while($row = mysqli_fetch_assoc($query)) {
          $id=$row['id'];
          $fname=$row['first_name']; 
          $lname=$row['last_name'];
          $dep = $row['department']; 
          $pos = $row['position']; 
          $repby = $row['first_name'] . " " . $row['last_name']; 
          $repdep = $row['department'];
          $reppos = $row['position'];
      } 
   } 
  mysqli_select_db($con, $db_name);
  $edit= "SELECT * FROM reports WHERE rep_id = '{$_GET['rep_id']}'";
  $result = mysqli_query($con, $edit) or die(mysqli_error($con));
  $row2 =  mysqli_fetch_array($result);

  if(isset($_POST['update'])) {
      $repid = $_POST['repid'];
      $reporttype = $_POST['reporttype'];
      $report = $_POST['report'];
      $repto = $_POST['reportedto'];
      $sql = "UPDATE reports SET rep_type='$reporttype', report='$report', rep_to='$repto', rep_ledit_date=NOW() WHERE rep_id='$repid'";
      $retval = mysqli_query($con, $sql);
      mysqli_close($con);
      $success="You have successfully edited your report.";
  }

  ?>

然后在表单上,​​未设置reptype的值。我还添加了一个隐藏字段来检索rep_id。

  <form name="editor" action="edit-this-report.php?rep_id=<?php echo $_GET['rep_id']; ?>" method="post" >
   <p class="inline">
    <span>
       <label for="mem">Reported by</label>
  <input type="text" name="reportedby" maxlength="20" disabled value="<?php print  $fname . " " . $lname; ?>" />
    </span>
   </p>
   <p class="inline">
     <span>
        <label for="mem">Department Name</label><input type="text" name="repdepart" disabled size="100" maxlength="100" value="<?php print $dep; ?>">
     </span>
   </p>
   <p class="inline">
      <span>
        <label for="mem">Position</label><input disabled type="text" name="repposition" size="100" value="<?php print $pos; ?>">
      </span>
   </p>
  <input name="repid" type="hidden" id="repid" value="<?php echo $row2['rep_id']; ?>">
   <p>
     <span>
     <select name="reporttype">
         <option value="Daily Report"<?php if ($row2['rep_type'] === 'Daily Report') echo ' selected="selected"'; ?>>Daily Report</option>
         <option  value="Weekly Report"<?php if ($row2['rep_type'] === 'Weekly Report') echo ' selected="selected"'; ?>>Weekly Report</option>
         <option value="Monthly Report"<?php if ($row2['rep_type'] === 'Monthly Report') echo ' selected="selected"'; ?>>Monthly Report</option>
         <option value="Quarterly Report"<?php if ($row2['rep_type'] === 'Quarterly Report') echo ' selected="selected"'; ?>>Quarterly Report</option>
         <option value="Annual Report"<?php if ($row2['rep_type'] === 'Annual Report') echo ' selected="selected"'; ?>>Annual Report</option>
         <option  value="Terminal Report"<?php if ($row2['rep_type'] === 'Terminal Report') echo ' selected="selected"'; ?>>Terminal Report</option>
     </select>
    <span>
   </p>

   <p>
    <span>
         <label for="mem">Report</label> 
         <textarea name="report" id="report" rows="23" cols="auto"><?php echo $row2['report'];?></textarea>
    <span>
   </p>
   <p>
    <span>
        <label for="mem">Reported to</label> 
        <select name="reportedto">
  <?php
  require ("includes/db.php");
        $q2= "SELECT * FROM users WHERE department like '%$repdep%'"; 
        $result3=mysqli_query($con, $q2) or die(mysqli_error($con));
        while ($getuser=mysqli_fetch_array($result3)){
        $repto=$getuser['first_name'] . " " . $getuser['last_name'];
  ?>
          <option value="<?php echo $repto; ?>"><?php echo $repto; ?></option>; 
        <?php       
        }       
        ?>

        </select>
    </span>
   </p>
    <span>
        <input name="update" type="submit" class="btn btn-large btn-primary" id="report_button" value="Submit Report" > 
        <input name="cancel" type="reset" class="btn btn-large btn-secondary" id="report_button" value="Cancel All Changes" >
    </span>
  </form>
   </p>

我希望这有助于其他访客。谢谢bloodyKnuckles和Mark的帮助!