看起来一切都与此代码工作正常,但实际上无法更新数据库,在获取数据时可以正确显示数据,但是当我按下update按钮时,数据消失了,但未执行任何更新。对我来说看起来不错,但似乎我错了。
这是我教授的一个项目,因此我不关心SQL注入和其他注入。

<html>
<head>
  <link rel="stylesheet" type="text/css" href="btnstyle.css">
  <title>Managament System</title>
</head>

<body>

  <h1>TU Chemnitz Student managament system</h1>

  <br>
  <a href="insert_form.html" class="myButton">ADD Person</a>
  <a href="insert_form.html" class="myButton">Edit Person</a>
  <a href="insert_form.html" class="myButton">Manage Boards</a>
  <a href="insert_form.html" class="myButton">Manage Departments</a>
  <a href="search_personel.html" class="myButton">Search N&S</a>
  <a href="three_search.html" class="myButton">Triple Search</a>
  <a href="mtable.php" class="myButton">Membership</a>

  <br>
  <br>

<?php

// set database server access variables:
$host = "localhost";
$user = "";
$pass = "";
$db = "";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$querys = "SELECT * FROM tblperson";



// execute query
$result = mysql_query($querys) or die ("Error in query: $query. ".mysql_error());


echo "<table border=1 align=center>
<tr>
<th>Personal ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Deparment</th>
<th>Board</th>
<th>Marticulation Number</th>
<th>Reg Date</th>
<th>Action</th>

</tr>";
while($row = mysql_fetch_array($result)) {
    ?>
    <?php
         echo '<tr>';
         echo '<td>'. $row['personid'].'</td>';
         echo '<td>'. $row['personname'].'</td>';
         echo '<td>'. $row['personsurname'].'</td>';
         echo '<td>'. $row['persondepartment'].'</td>';
         echo '<td>'. $row['personboard'].'</td>';
         echo '<td>'. $row['martinumber'].'</td>';
         echo '<td>'. $row['personregdate'].'</td>';
         echo '<td>'.'<a href="edit20.php?edit='.$row['personid'] . '"> EDIT </a> '.'</td>';

}
?>
</body>
</html>


这是似乎有问题的编辑文件。

<?php

include_once('coneksioni.php');

if(isset($_GET['edit']))
{
        $personid = $_GET['edit'];
        $res = mysql_query("SELECT * FROM tblperson WHERE personid='$personid'");
        $row = mysql_fetch_array($res);
}
        if(isset($_POST['newpersonname']))
        {
            $newpersonname = $_POST['newpersonname'];
            $personid = $_POST['personid'];
            $sql = "UPDATE tblperson SET personname = '$newpersonname' WHERE personid = '$personid'";
            $res = mysql_query($sql) or die ("Cant be updated");
            echo "< meta http-equiv='refresh' content='0;url=home.php'>";
        }
?>


  <form action="edit20.php" method="POST">
    <table border="0">
      <tr>
        <td>First Name</td>
         <td><input type="text" name="newpersonname" value="<?php echo $row[1];?>" maxlength="30" size="13"></td>
      </tr>
      <tr>
        <td>Last Name</td>
        <td> <input type="text" name="personsurname" value="<?php echo $row[2];?>" maxlength="30" size="30"></td>
      </tr>
      <tr>
        <td>Department</td>
        <td>
            <select name='persondepartment'>
            <option>Production</option>
            <option>Sales</option>
        </select>
        </td>
      </tr>
      <tr>
        <td>Board</td>
        <td>
            <select name='personboard'>
            <option>Evaluation</option>
            <option>Executive</option>
            <option>Research</option>
        </select>
        </td>
      </tr>
      <tr>
        <td>Marticulation Number</td>
        <td> <input type="text" name="martinumber" maxlength="60" size="30"></td>
      </tr>
      <tr>
        <td>Date of Registration</td>
        <td><input type="date" name="personregdate" maxlength="7" size="7"></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value=" Update"></td>
      </tr>
    </table>
  </form>

最佳答案

当您在edit20.php中的表单上按下“更新”按钮时,您正在寻找personid,但该值从未设置过,因此它将为空并且更新将失败。



  <form action="edit20.php" method="POST">


加:

<input type="hidden" name="personid" value="<?php echo $personid; ?>">

关于php - 无法更新管理员输入的新数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38566317/

10-11 03:26
查看更多