单击按钮后,PHP编辑字段未在sql中更新

时间:2018-12-02 22:48:59

标签: php html mysql session bootstrap-4

使用sqlphp进行小型学校项目。在应用程序中,我有一个Edit按钮,该按钮允许用户更新字段doctorFname,doctorLname, idDepartment, and specialty。用户输入新数据并单击save时,它应发布到mysql中。这是我项目的最后一步,我很难弄清为什么它没有更新。我能够delete并添加new个用户。我将在下面发布代码陶氏。任何帮助将不胜感激,因为我离完成仅一步之遥。再次感谢!

更新:我已经打开error reporting,这是我收到的错误。 Cannot modify header information - headers already sent by (output started at /Users/moe/Desktop/CRUD/index.php:1) in /Users/moe/Desktop/CRUD/formprocessor.php on line 87

更新2:好的,在用户单击UPDATE按钮后查看了我的update语句之后,我忘记在'语句中添加sql。在sql workbench中运行相同的查询并使其正常运行后,我尝试查看是否最终正确。但是然后我得到Incorrect integer value: '' for column 'idDepartment' at row 1

以下的错误

更新3:我尝试使用以下正则表达式获取integer的{​​{1}}值,因为idDepartment期望mysql但收到{{1 }}。这没有帮助,我仍然面临问题。 int

string

$idDepart = preg_replace("/[^0-9,.]/", "", $idDepart);

Doctor Table

Table: Doctor

Columns:
doctorID      int(11) AI PK
doctorFName   varchar(45)
doctorLname   varchar(45)
idDepartment  int(11)
specialty     varchar(45)

index.php

<!doctype html>
<html lang="en">
  <head>
    <title>Title</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  </head>
  <body>
  <?php require_once 'formprocessor.php';?>
  <!-- Based on query type it will use bootstrap alert class -->
  <?php
    if (isset($_SESSION['message'])): ?>
    <div class = "alert alert-<?=$_SESSION['msg_type'] ?>">

    <?php
        // Based on the message type it will echo it and then unset the session
        echo $_SESSION['message'];
        unset($_SESSION['message']);
        ?>

    </div>
    <!-- End of if statment -->
    <?php endif ?>
  <div class = "container">

  <?php
    // Connect to databse 
    $mysqli = new mysqli('127.0.0.1', 'root', "", 'v2HospitalDB')
    or die(mysqli_error($mysqli));
    // Will select everthing from Doctor and display on the page
    $result = $mysqli->query("SELECT * FROM Doctor") or die($mysqli->error);
    ?>


    <!-- This is the format for the table with the folowing columns -->
    <div class="row justify-content-center">
        <table class="table">
            <thead>
                <tr>
                 <th> Doctor ID </th>
                 <th> First Name </th>
                 <th> Last Name</th>
                 <th> Department ID</th>
                 <th> Speciality </th>
                 <th colspan="2"> Action </th>
                </tr>
            </thead>

            <?php
    // everything is fetched from db and stored in row
      while($row = $result->fetch_assoc()):?>
        <!-- Each row will have its repsected column from the database -->
            <tr>
            <td> <?php echo $row['doctorID']; ?></td>
            <td> <?php echo $row['doctorFName']; ?></td>
            <td> <?php echo $row['doctorLname']; ?></td>
            <td> <?php echo $row['idDepartment']; ?></td>
            <td> <?php echo $row['specialty']; ?></td>


            <td>
                <a href="index.php?edit=<?php echo $row['doctorID']; ?>"
                    class="btn btn-info">Edit</a>
                <a href="formprocessor.php?delete=<?php echo $row['doctorID'];?>"
                     class="btn btn-danger">Delete</a>

            </td>
            </tr>

            <?php endwhile;
            ?>


        </table>

    </div>

    <?php

    // This function prints the array in a nice format
    function pre_r($array) {
        echo '<pre>';
        print_r($array);
        echo '</pre>';
    }
    ?>

    <div class="row justify-content-center">
    <form action="formprocessor.php" method="post">

    <!-- hidden input field for the update -->
    <input type="hidden" name="id" value="<?php echo $id; ?>">

    <div class="form-group">
    <label> First Name</label>
    <input type="text" name="fname" class="form-control" value="<?php echo $firstname;?>" placeholder="Enter First Name">
    </div>

    <div label="form-group">
    <label> Last Name</label>
    <input type="text" value="<?php echo $lastname;?>" name="lname" class="form-control" placeholder="Enter Last Name">
    </div>

    <div label="form-group">
    <label> Department ID</label>
    <input type="text" value="<?php echo $idDepart;?>" name="departmentID" class="form-control" placeholder="Enter DepartmentID">
    </div>

    <div label = "form-group">
    <label> Speciality </label>
    <input type="text" value="<?php echo $special;?>" name="speciality" class="form-control" placeholder="Enter Specialty">
    </div>

    <div class="form-group">
    <?php 
    if($update == true): ?>
    <button class="btn btn-info" type="submit" name="update">Update</button>
<?php else: ?>
    <button class="btn btn-primary" type="submit" name="save">Save</button>
<?php endif; ?>
    </div>

    </form>
    </div>
    </div>
  </body>
</html>

0 个答案:

没有答案