2016-12-12 9 views
0

3日間このクエリを試しています。ここに行のリストがあります:http://prntscr.com/dick00。私がしたいのは、それぞれの行を編集して削除することだけです。何らかの理由で、IDが転送されず、レコードが更新されていません。idがforwardでない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 EDIT 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(); 

     } 
    ?> 

EDIT 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 --> 

もっとコードを要求してくれてありがとう...私は十分なコードサンプルを与えられたと思います。

+0

もっと説明できますか?このコードでは、データベースの行を更新しようとしていますが、何を期待していますか? –

+0

エラーは 'access.php'にありますが、その行には表示されません。コードをもっと表示する必要があります。基本的に、$ row ['id']は存在しません(selectクエリやPHPサイクルでエラーが発生する可能性があります) –

+0

私はあなたに立つことができません......この情報を更新するためのフォームはありますか?コードをコピーする代わりに問題を説明しなければなりません –

答えて

0
あなたが最もこのようなあなたのHTMLフォームで隠し入力の内側あなたのIDを入れ

:あなたのフォームを送信するときに、サイドでのitemIdを持って

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

、その後を$ _POST [」 itemId ']変数。

編集: 私はあなたのためのシナリオを記述する必要があります。多分あなたはポイントを持っています。次のあなたの編集アクセスで

<form method="post" action="edit-access.php"> 
    ..... 
    <input type="hidden" name="id" value="<?php echo $_GET['id']?>"> 
    ..... 
</form> 

:あなたはこのような構造を持つフォームを持っているあなたのアクセスform.phpで

<a href="access-form.php?id=<?php echo $_GET['id']?>">access ....</a> 

: あなたはすべての行にアクセス魔女のリストを持っていることは、このタグを持っています.phpこのIDでこの構文でアクセスできます:

+0

十分な情報コードを与えないと申し訳ありませんが、これを反映するように編集します。 @ Navid_pdp11 ...投稿する前に隠れた入力をどこに置くのですか? – Olaskee

+0

編集パネル内のどこに....名前とその他の情報が入っているテキストボックスの横にある –

+0

こんにちは。私の更新コードを確認してください。 – Olaskee

関連する問題