首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用id更新未向前执行id的行数据

使用id更新未向前执行id的行数据
EN

Stack Overflow用户
提问于 2016-12-12 07:54:26
回答 1查看 166关注 0票数 0

已经尝试这个查询3天了。这里有一个行列表:http://prntscr.com/dick00。我想要的是分别编辑和删除每一行。由于某些原因,id没有传递,也没有更新记录。

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

这是我在access.php中的链接

代码语言:javascript
复制
                  <td><a href="edit_access.php?id=<?php echo $row['id']; ?>"><i class="fa fa-edit"></i>edit</td>      

edit_access.php 编辑1: php代码

代码语言:javascript
复制
        <?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

代码语言:javascript
复制
                    <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 -->

谢谢你要求更多的代码..。我希望能给出足够的代码样本。

EN

回答 1

Stack Overflow用户

发布于 2016-12-12 11:36:43

大多数情况下,在html表单中将id放在隐藏的输入中,如下所示:

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

然后,当您提交表单时,侧$_POST'itemId'变量中有itemId

编辑:我必须为您描述场景。也许你明白重点了。在每一行中都有一个访问女巫列表,其中有以下标记:

代码语言:javascript
复制
<a href="access-form.php?id=<?php echo $_GET['id']?>">access ....</a>

在access-form.php中,有一个具有以下结构的表单:

代码语言:javascript
复制
<form method="post" action="edit-access.php">
    .....
     <input type="hidden" name="id" value="<?php echo $_GET['id']?>">
    .....
</form>

接下来,在您的编辑- access . you中,您可以通过以下语法访问这个id:

代码语言:javascript
复制
echo $_POST['id'];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41096339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档