2016-10-17 1 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(); 

      } 
       ?> 

* 내 PHP입니다 그리고 것은 내 HTML입니다 *

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

여기 내 업데이트 된 코드는 희망을 일으키는 무언가가이다.

+1

먼저' $ competency'입니다. '$ competency'를 어디에서 정의 했습니까? – Mike

+0

네, 이미 여기에'$ competency = implode (',', $ _POST [ 'competency']);'나는 이것을 데이터베이스에 게시하는 데 사용했습니다 – Olaskee

+1

in_array는 문자열에서 작동하지 않습니다. 이름에서 알 수 있듯이 배열에서 작동합니다. – Mike

답변

0

이 (업데이트)보십시오 :

<?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는 [ '역량']`다음 방금이

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

귀하의 의견을 보내 주셔서 감사합니다, 내 방법은 게시물, 나는 또한 당신을 시도 할 것입니다. – Olaskee

+0

응답이 늦어서 죄송합니다. 코드 제안을 시도했지만 여전히 동일한 문제가 발생했습니다. 내 업데이트 된 코드에주의하십시오. 문제가있는 곳을 볼 수도 있습니다. – Olaskee

+0

업데이트 된 코드 확인 – Okba