PHP无法更新数据库,提交后没有任何反应

时间:2016-09-16 09:12:45

标签: php mysql

这是我的表单页面

<?php     $result= mysql_query("SELECT * FROM Staff WHERE StaffID = 1");

while ($myrow=mysql_fetch_assoc($result))
{

$PName=$myrow['PName'];
$PGender=$myrow['PGender'];
$PCoNum=$myrow['PCoNum'];
$PEmail=$myrow['PEmail'];
$PID=$myrow['PID'];
$PNat=$myrow['PNat'];
$PAdd=$myrow['PAdd'];
$PSal=$myrow['PSal'];
}
?>
  <form action="SavePProfile.php" method="POST">
    <div class="form-group">
      <label for="PName" class="col-sm-2 control-label">Name: </label>
      <div class="col-sm-5">
        <input type="text" class="form-control" value= "<?php echo $PName; ?>" id="PName">
      </div>
    </div>
    <br></br>
    <div class="form-group">
      <label for="PGender" class="col-sm-2 control-label">Gender: </label>
      <div class="col-sm-5">
        <input type="text" class="form-control" value= "<?php echo $PGender; ?>" id="PGender">
      </div>
    </div>
    <br></br>
    <div class="form-group">
      <label for="PCoNum" class="col-sm-2 control-label">Contact No: </label>
      <div class="col-sm-5">
        <input type="text" class="form-control" value= "<?php echo $PCoNum; ?>" id="PCoNum">
      </div>
    </div>
    <br></br>
    <div class="form-group">
      <label for="PEmail" class="col-sm-2 control-label">Email Address: </label>
      <div class="col-sm-5">
        <input type="text" class="form-control" value= "<?php echo $PEmail; ?>" id="PEmail">
      </div>
    </div>
    <br></br>
    <div class="form-group">
      <label for="PID" class="col-sm-2 control-label">IC/Passport No: </label>
      <div class="col-sm-5">
        <input type="text" class="form-control" value= "<?php echo $PID; ?>" id="PID">
      </div>
    </div>
    <br></br>
    <div class="form-group">
      <label for="PNat" class="col-sm-2 control-label">Nationality: </label>
      <div class="col-sm-5">
        <input type="text" class="form-control" value= "<?php echo $PNat; ?>" id="PNat">
      </div>
    </div>
    <br></br>
    <div class="form-inline">
      <label for="PAdd" class="col-sm-2 control-label">Address: </label>
      <textarea class="form-control" rows="5" cols="68" id="PAdd"><?php echo $PAdd; ?></textarea>
    </div>
    <br></br>
    <div class="form-group">
      <label for="PSal" class="col-sm-2 control-label">Salary (Monthly): </label>
      <div class="col-sm-5">
        <input type="text" class="form-control" value= "<?php echo $PSal; ?>" id="PSal">
      </div>
    </div>
    <br></br>
  <button type="submit" class="btn btn">Save</button>
  <button type="submit" class="btn btn" onclick="location.href='HomePage.php';">Back</button>
  </form>

虽然这是我的更新过程

  <?php    if (isset($_POST['PName']))
  {
      $PName=$_POST['PName'];
      $PGender=$_POST['PGender'];
      $PCoNum=$_POST['PCoNum'];
      $PEmail=$_POST['PEmail'];
      $PID=$_POST['PID'];
      $PNat=$_POST['PNat'];
      $PAdd=$_POST['PAdd'];
      $PSal=$_POST['PSal'];




      $UpdateProfile="UPDATE Staff
      SET PName= '$PName', PGender= '$PGender', PCoNum= '$PCoNum', PEmail='$PEmail', PID='$PID', PNat='$PNat', PAdd='$PAdd', PSal='$PSal'
      WHERE StaffID=1";

      if (empty ($_POST['PName'])||empty ($_POST['PGender'])||empty ($_POST['PCoNum'])||empty ($_POST['PEmail'])||empty ($_POST['PID'])||empty ($_POST['PNat'])||empty ($_POST['PAdd'])||empty ($_POST['PSal']))
        {
            echo '<div class="callout callout-danger">
            <h4>Notice!</h4>
            <p>Unable to save!</p>
            </div>';
        }
        else 
        {
            mysql_query($UpdateProfile);

            echo '<div class="callout callout-success">
            <h4>Notice!</h4>
            <p>User saved.</p>
            </div>';
        }


  }
  ?>
  <br></br>

数据能够显示在表单页面中,但是当我尝试单击“保存”按钮时,没有任何反应,没有通知没有错误,有人可以告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:1)

您的所有输入都没有name属性,您应该更改:

<input type="text" class="form-control" value= "<?php echo $PName; ?>" id="PName">

使用:

<input type="text" class="form-control" value= "<?php echo $PName; ?>" name="PName">

等等。

编辑:当然,您不应该使用mysql_*函数,如here所述。