更新ID不带ID前进

时间:2016-12-12 07:54:27

标签: php mysqli prepared-statement fetch

现在正在尝试此查询3天。我在这里有一个行列表:http://prntscr.com/dick00。我想要的是分别编辑和删除每一行。由于某种原因,id没有结转,没有记录正在更新。

当我点击access.php中的编辑时,我在地址栏中显示edit_access.php?id=

以下是 access.php

中的链接
                  <td><a href="edit_access.php?id=<?php echo $row['id']; ?>"><i class="fa fa-edit"></i>edit</td>      

edit_access.php 编辑1:php代码

        <?php

    // start session
    session_start();
    //   error_reporting(E_ALL); ini_set('display_errors', 1);

    if(!isset($_SESSION['user_type'])){
    header('Location: index.php');
    }

    // include connection
    require_once('include/connection.php');

    // set user session variables
    $userId = $_SESSION['user_id'];
    $error = [] ;
    if(isset($_POST['update']))   
    {                 
    $id = $_POST['id'];

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $therapist = $_POST['therapist'];
    $access_type = $_POST['access_type'];
    $code = $_POST['code']; 
    $created_at = $_POST['created_at'];        
    $postcode = $_POST['postcode'];  

    // validate form field
    if (empty($firstname)){     
                  $error[] = 'Field empty, please enter patient first name';        
                }
    if (empty($lastname)){     
                  $error[] = 'Field empty, please enter patient last name';        
                }

    if (empty($therapist)){     
                  $error[] = 'Field empty, please enter your name';                      
                 // $error = true;
                }
    if (empty($code)){     
                  $error[] = 'Field empty, please enter patient access code';                      
                 // $error = true;
                } 
     if (empty($access_type)){     
                  $error[] = 'Field empty, please check access type';                      
                 // $error = true;
                } 
      if (empty($postcode)){     
                  $error[] = 'Field empty, please enter patient postcode';                      
                 // $error = true;
                } 



            //if no errors have been created carry on
    if(empty($error)){         

    $updated_at = date('Y-m-d');
    // ************* UPDATE PROFILE INFORMATION ************************//
    if(!($stmt = $con->prepare("UPDATE access SET firstname = ?, lastname = ?, therapist = ?, access_type = ?, postcode = ?, code = ?, updated_at = ?
    WHERE id = ?"))) {
    echo "Prepare failed: (" . $con->errno . ")" . $con->error;
    }
    if(!$stmt->bind_param('sssssssi', $firstname, $lastname, $therapist, $access_type, $postcode, $code, $updated_at, $userId)){
    echo "Binding paramaters failed:(" . $stmt->errno . ")" . $stmt->error;
        }

    if(!$stmt->execute()){
    echo "Execute failed: (" . $stmt->errno .")" . $stmt->error;
    }

    if($stmt) {
    $_SESSION['main_notice'] = '<div class="alert alert-success">"Access Code Added successfully!"</div>';
    header('Location: access.php');
    exit;

    }else{
        $_SESSION['main_notice'] = '<div class="alert alert-danger">"Some error, try again"</div>';
        header('Location: '.$_SERVER['PHP_SELF']);
    }

    }                      

    }       


    // title page
    $title = "Edit Access Record | Allocation | The Whittington Center";

    // include header
    require_once('include/header.php');
    ?>

    <?php   
      if(isset($_GET['id'])){
      $userId = $_GET['id'];
      }
      else{ 
      $userId = $_POST['user_id'];

      //  mysqli_close($con);
      $stmt = $con->prepare("SELECT * FROM access WHERE id = ?");
      $stmt->bind_param('s', $userId);
      $stmt->execute();
      $stmt->store_result();  
      if($stmt->num_rows == 0) {  
      echo 'No Data Found for this user';
      }else {
      $stmt->bind_result($firstname, $lastname, $therapist, $access_type, $postcode, $code);
      while ($row = $stmt->fetch());

      $stmt->close();

      }
    ?>

编辑2:HTML部分

                    <h2 class="text-light text-greensea">Edit Access Record</h2>


                    <form name="access" class="form-validation mt-20" novalidate="" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" autocomplete='off'>


                      <div class="form-group">
                        <input type="text" name="firstname" class="form-control underline-input" value='<?php if(isset($error)){ echo $_POST[' firstname ']; } ?>' placeholder='firstname'></td>
                      </div>
                      <div class="form-group">
                        <input type="text" name="lastname" class="form-control underline-input" value='<?php if(isset($error)){ echo $_POST[' lastname ']; } ?>' placeholder='lastname'></td>
                      </div>
                      <div class="form-group">
                        <input type="text" name="therapist" class="form-control underline-input" value='<?php if(isset($error)){ echo $_POST[' therapist ']; } ?>' placeholder='therapist'></td>
                      </div>

                      <?php $access_type = $access_type; ?>
                      <div class="form-group ">
                        <label for="work status">Access Type</label>
                        <div name="access_type" value='<?php if(isset($error)){ echo $_POST[' access_type ']; } ?>'>

                          <label class="checkbox-inline checkbox-custom">
                            <input type="checkbox" name="access_type" <?php if (isset($work_status) && $access_type == "Keysafe") echo "checked"; ?> value="Keysafe"><i></i>Keysafe
                        </label>
                          <label class="checkbox-inline checkbox-custom">
                            <input type="checkbox" name="access_type" <?php if (isset($access_type) && $access_type == "keylog") echo "checked"; ?> value="keylog"><i></i>Keylog
                        </label>

                        </div>
                      </div>
                      <div class="form-group">
                        <input type="text" name="code" class="form-control underline-input" value='<?php if(isset($error)){ echo $_POST[' code ']; } ?>' placeholder='access code'></td>
                      </div>
                      <div class="form-group">
                        <input type="text" name="postcode" class="form-control underline-input" value='<?php if(isset($error)){ echo $_POST[' postcode ']; } ?>' placeholder='postcode'></td>
                      </div>

                      <div class="form-group text-left mt-20">
                        <button type="update" class="btn btn-primary pull-right" name="update" id='update'>Add Access</button>
                        <!--                             <label class="checkbox checkbox-custom-alt checkbox-custom-sm inline-block">
            <input type="checkbox"><i></i> Remember me
        </label> -->
                        <a href="access.php">
                          <button type="button" class="btn btn-greensea b-0 br-2 mr-5">Back</button>
                        </a>
                      </div>

                    </form>



                </div>
                <!-- end of container -->

感谢大家要求提供更多代码...我希望已经提供了足够的代码示例。

1 个答案:

答案 0 :(得分:0)

你最喜欢把你的id放在html表单中的隐藏输入中,如下所示:

<input type="hidden" name="itemId" value="<?php echo '$_GET['id']'?>">

然后当您提交表单时,您在 $ _ POST ['itemId']​​ 变量中有 itemId

编辑: 我必须为你描述一下情景。也许你明白了。 你有一个访问列表,每行都有这个标记:

<a href="access-form.php?id=<?php echo $_GET['id']?>">access ....</a>
在你的access-form.php中,你有一个具有这种结构的表格:

<form method="post" action="edit-access.php">
    .....
     <input type="hidden" name="id" value="<?php echo $_GET['id']?>">
    .....
</form>
在您的edit-access.php中,您可以通过以下语法访问此ID:

echo $_POST['id'];