2016-10-17 5 views
0

複数のチェックボックスの配列の値を保持できません。このサイトを見て、私の答えを適用しましたが、何らかの理由でチェックされた値を保持していません。 **ここに私のPHPは*複数のチェックボックスの値を保持している問題があります。

 <?php 

    // start session 
    session_start(); 

    // i user is not log in redirect back to index 
    if(!isset($_SESSION['user_type'])){ 
     header('Location: index.php'); 
    } 

    // include data based connection config 
    include_once('include/connection.php'); 

    // incluude header ttile page 
    $title = "Edit User Profile"; 

    // set user session variables 
    $userId = $_SESSION['user_id']; 
    //$userName = $_SESSION['user_name']; 
    $temp = $_SESSION['user_name']; 

    //$competency = ""; 
    // if user update the data 
     if(isset($_POST['update'])){ 

     // define post variable 
    $firstname = trim($_POST['firstname']); 
    $lastname = trim($_POST['lastname']); 
    $email = trim($_POST['email']); 
    $user_name = trim($_POST['user_name']); 
    $profession = trim($_POST['profession']); 
    $phone = trim($_POST['phone']); 
    $hobbies = trim($_POST['hobbies']); 
    $skills = trim($_POST['skills']); 
    $competency = implode(', ', $_POST['competency']); 
    $postcode = trim($_POST['postcode']); 
    $address = trim($_POST['address']); 
    $bio = trim($_POST['bio']); 
    $dob = trim($_POST['dob']); 
    $gender = trim($_POST['gender']); 
    $country = trim($_POST['country']); 


      // ************* UPDATE PROFILE INFORMATION ************************// 
      if(!($stmt = $con->prepare("UPDATE user SET firstname = ?, lastname = ?, skills = ?, competency = ?, profession = ?, 
      user_name = ?, phone = ?, address = ?, email = ?, bio = ?, 
      gender = ?, dob = ?, country = ? WHERE id = ?"))) { 
       echo "Prepare failed: (" . $con->errno . ")" . $con->error; 
      } 
      if(!$stmt->bind_param('ssssssissssssi', $firstname, $lastname, $skills, $competency, $profession, 
           $user_name, $phone, $address, $email, $bio, 
           $gender, $dob, $country, $userId)){ 
      echo "Binding paramaters failed:(" . $stmt->errno . ")" . $stmt->error; 
        } 
      if(!$stmt->execute()){ 
        echo "Execute failed: (" . $stmt->errno .")" . $stmt->error; 
        } 


        if($stmt) { 
         $_SESSION['main_notice'] = "Successfully Updated!"; 
            header('Location: profile.php'); 
             exit; 

        }else{ 
          $_SESSION['main_notice'] = "Some error, try again"; 
          header('Location: '.$_SERVER['PHP_SELF']); 
         } 

      $stmt->close(); 



     } 

       $stmt = $con->prepare("SELECT firstname, lastname, skills, competency, user_name, avatar, profession, email, dob, gender, country, phone, bio, address, created_at FROM user 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, $skills, $competency, $user_name, $avatar, $profession, $email, $dob, $gender, $country, $phone, $bio, $address, $created_at); 
       $stmt->fetch(); 

       $stmt->close(); 

      } 
       ?> 

**であり、ここで*

 <div class="user-details"> 

     <form name="update" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" id="UploadForm"> 

    <table> 
    <label for="">Avatar</label> 
    <input name="ImageFile" type="file" id="uploadFile"/> 
    <!--      <div> 
     <div class="shortpreview"> 
      <label for="">Previous Avatar </label> 
      <br> 
      <img src="userfiles/avatars/<?php //echo $avatar;?>" width='150' height='150' > 
     </div> 
    </div> --> 
    <div class="col-md-6"> 
     <div class="shortpreview" id="uploadImagePreview"> 
      <label for="">Current Uploaded Avatar </label> 
      <br> 
      <div id="imagePreview"></div> 
     </div> 
    </div> 
    </table> 


     <table> 
     <tr> 
      <td></td> 
      <td></td> 
      <td>Update</td> 
     </tr> 
     <tr> 
     <td><label for = "">First Name</label></td> 
     <td><input type="text" id="firstname" name="firstname" value="<?php echo $firstname; ?>"></td> 
     </tr> 

     <tr> 
     <td><label for = "">Last Name</label></td> 
     <td><input type="text" id="lastname" name="lastname" value ="<?php echo $lastname; ?>"></td> 
     </tr> 

     <tr> 
     <td><label for = "">Skills</label></td> 
     <td><input type="text" id="skills" name="skills" value ="<?php echo $skills; ?>"></td> 
     </tr> 
    <tr> 
    <td>   
    <label >Competency:</label> 
    <input type="checkbox" value="RRT" name="competency[]" <?php if(isset($_POST['competency']) && in_array('RRT', $_POST['competency'])) echo 'checked="checked"'; ?>>RRT<br> 
    <input type="checkbox" value="ESD" name="competency[]" <?php if(isset($_POST['competency']) && in_array('ESD', $_POST['competency'])) echo 'checked="checked"'; ?>>ESD<br> 
    <input type="checkbox" value="SDT" name="competency[]" <?php if(isset($_POST['competency']) && in_array('SDT', $_POST['competency'])) echo 'checked="checked"'; ?>>SDT<br> 
    <input type="checkbox" value="CAR" name="competency[]" <?php if(isset($_POST['competency']) && in_array('CAR', $_POST['competency'])) echo 'checked="checked"'; ?>>CAR <br> 
    <input type="checkbox" value="DH" name="competency[]" <?php if(isset($_POST['competency']) && in_array('DH', $_POST['competency'])) echo 'checked="checked"'; ?>>DH<br> 
    <input type="checkbox" value="MEDS" name="competency[]" <?php if(isset($_POST['competency']) && in_array('MEDS', $_POST['competency'])) echo 'checked="checked"'; ?>>MEDS <br> 

    <!--     <input type="checkbox" value="RRT" name="competency['RRT']" <?php// if(!empty($_POST['competency']['RRT'])) echo "checked = 'checked'"; ?>>RRT<br> 
    <input type="checkbox" value="ESD" name="competency['ESD']" <?php //if(!empty($_POST['competency']['ESD'])) echo "checked = 'checked'"; ?>>ESD<br> 
    <input type="checkbox" value="SDT" name="competency['SDT']" <?php //if(!empty($_POST['competency']['SDT'])) echo "checked = 'checked'"; ?>>SDT<br> 
    <input type="checkbox" value="CAR" name="competency['CAR']" <?php //if(!empty($_POST['competency']['CAR'])) echo "checked = 'checked'"; ?>>CAR<br> 
    <input type="checkbox" value="MEDS" name="competency['MEDS']" <?php// if(!empty($_POST['competency']['MEDS'])) echo "checked = 'checked'"; ?>>MEDS<br> 
    <input type="checkbox" value="DH" name="competency['DH']" <?php //if(!empty($_POST['competency']['DH'])) echo "checked = 'checked'"; ?>>DH<br> --> 
    </td> 
    </tr> 
    <!--   <tr>   
    <td> 

    <select name="competency[]" multiple="multiple"> 
    <option value="SDT">SDT</option> 
    <option value="ESD">ESD</option> 
    <option value="CAR">CAR</option> 
    <option value="MEDS">MEDS</option> 
    <option value="DH">DH</option> 
    </select> 

    </td> 
    </tr> --> 

     <tr> 
     <td><label for = "">User Nmae</label></td> 
     <td><input type="text" id="user_name" name="user_name" value="<?php echo $user_name; ?>"></td> 
     </tr> 

     <tr> 
     <td><label for = "">Profession Name</label></td> 
     <td><input type="text" id="profession" name="profession" value="<?php echo $profession; ?>"></td> 
     </tr>        

     <tr> 
     <td><label for = "">Phone</label></td> 
     <td><input type="text" id="phone" name="phone" value="<?php echo $phone; ?>"></td> 
     </tr>       

     <tr> 
     <td><label for = "">Email</label></td> 
     <td><input type="text" id="email" name="email" value="<?php echo $email; ?>"></td> 
     </tr> 

     <tr> 
     <td><label for = "">Gender</label></td> 
     <td><input type="text" id="gender" name="gender" value="<?php echo $gender; ?>"></td> 
     </tr> 

     <tr> 
     <td><label for = "">Date Of Birth</label></td> 
     <td><input type="text" id="dob" name="dob" value="<?php echo $dob; ?>"></td> 
     </tr> 


     <tr> 
     <td><label for = "">Addres</label></td> 
     <td><input type="text" id="address" name="address" value="<?php echo $address; ?>"></td> 
      </tr> 


     <tr> 
     <td><label for = "">Country</label></td> 
     <td><input type="text" id="country" name="country" value="<?php echo $country; ?>"></td> 
     </tr> 


     <tr> 
     <td><label for = "">Bio</label></td> 
     <td><input type="text" id="bio" name="bio" value="<?php echo $bio; ?>"></td> 
     </tr> 

     <tr> 
     <td></td>    
     <td><input type="submit" id="update" name="update" value="Update"></td> 
     </tr> 
     </table> 
     </form>  
    </div> 

私のHTMLは、ここに私の更新されたコードは、うまくいけば、それを引き起こして何かがありますされています。

+1

まずあなたは '$ _POST [ '能力が']'、あなたが 'ちょうど持っています

<?php $competency = explode(', ', $competency); ?> <tr> <td> <label >Competency:</label> <input type="checkbox" value="RRT" name="competency[]" <?php if(is_array($competency) && in_array('RRT', $competency)) echo 'checked="checked"'; ?>>RRT<br> <input type="checkbox" value="ESD" name="competency[]" <?php if(is_array($competency) && in_array('ESD', $competency)) echo 'checked="checked"'; ?>>ESD<br> <input type="checkbox" value="SDT" name="competency[]" <?php if(is_array($competency) && in_array('SDT', $competency)) echo 'checked="checked"'; ?>>SDT<br> <input type="checkbox" value="CAR" name="competency[]" <?php if(is_array($competency) && in_array('CAR', $competency)) echo 'checked="checked"'; ?>>CAR <br> <input type="checkbox" value="DH" name="competency[]" <?php if(is_array($competency) && in_array('DH', $competency)) echo 'checked="checked"'; ?>>DH<br> <input type="checkbox" value="MEDS" name="competency[]" <?php if(is_array($competency) && in_array('MEDS', $competency)) echo 'checked="checked"'; ?>>MEDS <br> </td> </tr> 

また、あなたのフォームメソッドがPOSTであるシュル作ります$ competency'です。どこでも '$ competency'を定義しましたか? – Mike

+0

はい私はそれを既に '$ competency = implode( '、'、$ _POST ['competency']);'私はこれをデータベースに投稿するために使用しました – Olaskee

+1

in_arrayは文字列では動作しません。名前の通り、配列で動作します。 – Mike

答えて

0

(更新)これを試してみてください:

<form methos="POST" ... 
+0

入力いただきありがとうございます、私の方法は投稿です、私もあなたのように試してみます。 – Olaskee

+0

応答が遅れて申し訳ありませんが、あなたのコードの提案を試みましたが、同じ問題が残っています。更新されたコードを気にしてください。 – Olaskee

+0

更新されたコードを確認してください – Okba

関連する問題