有问题保留多个复选框值php



我有麻烦保留我的多复选框数组的值。我看了一下这个网站,并把答案应用到我的网站上,但由于某种原因没有保留选中的值。**这是我的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();
            }
                ?>

*这是我的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>

这是我更新的代码,希望有什么东西导致它。

Try this (updated):

<?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" ...

相关内容

  • 没有找到相关文章

最新更新