2016-07-26 61 views
-2

我正在使用一個簡單的開/關滑動開關來更新我的數據庫中的字段。使用開/關複選框滑動開關來運行SQL查詢

我把它打開「沒有問題,但允許用戶把它關掉」似乎給我帶來麻煩。我在這個頁面上有多個開關,所以無論何時「提交」,都會將「開」值設置爲「關」,除非我刪除elseif語句。

它似乎代碼認爲$ _POST ['check-twitter']在打開時是「isset」,但是當我將它從打開到關閉時,它是「!isset」。

如果我將代碼更改爲「elseif(!isset ....)」,那麼當我再次訪問該頁面時,它不會將「on」位置識別爲「isset」,因此它會自動轉動它關了。這與被認爲是「isset」和「!isset」似乎不一致。

我希望交換機只在用戶更改它時更新數據庫,而且我需要交換機向用戶顯示它是「開」還是「關」。

// $twitter_check represents the field from the database which equals 'Yes' or 'No' 

<label class="switch"> 
    <input type="checkbox" name="check-twitter" <?php echo $twitter_check == 'Yes'?'checked':''; ?>> 
    <div class="slider"></div> 
</label> 

if(isset($_POST['check-twitter']) && $twitter_check == "No"){ 
    //SQL UPDATE $twitter_check = 'Yes' 
    } elseif(isset($_POST['check-twitter']) && $twitter_check == "Yes") { 
    //SQL UPDATE $twitter_check = 'No' 
    } 

<input class="button" type="submit" name="submit" value="Save"/> 
+0

其中/你是怎麼定義'$ twitter_check'?這裏沒有足夠的代碼,並且如果確實使用了post方法。 –

+0

@ Fred-ii-正如我在我的文章中所述,$ twitter_check表示數據庫中等於「是」或「否」的字段。在我的頁面上顯然有更多的代碼顯示SQL查詢和表單,但它與理解我遇到的問題並不相關,我只是試圖在存在問題的位置展示代碼。 – Shane

回答

0

我得到它的工作...

<?php 

$sql = "SELECT * FROM XXXXXXX WHERE user_id = $id"; 

$result = mysqli_query($connection, $sql); 
$row = mysqli_fetch_array($result); 

$twitter_check = $row['auto_post_twitter']; 
$facebook_check = $row['fb_auto_post']; 
$schedule_check = $row['weekly_email']; 

    if(isset($_POST['submit'])){ 

     // CHECK TWITTER 
     if(isset($_POST['check-twitter']) && $twitter_check == "No"){ 
     $sql_twitter = "UPDATE XXXXXXXX SET auto_post_twitter = 'Yes' WHERE user_id = $id"; 

     $result_twitter = mysqli_query($connection, $sql_twitter); 
     $row_twitter = mysqli_fetch_array($result_twitter); 

     } elseif(!isset($_POST['check-twitter']) && $twitter_check == "Yes") { 
     $sql_twitter2 = "UPDATE XXXXXXXX SET auto_post_twitter = 'No' WHERE user_id = $id"; 

     $result_twitter2 = mysqli_query($connection, $sql_twitter2); 
     $row_twitter2 = mysqli_fetch_array($result_twitter2); 
     } 

     // CHECK FACEBOOK 
     if(isset($_POST['check-facebook']) && $facebook_check == "No"){ 
     $sql_facebook = "UPDATE XXXXXXX SET fb_auto_post = 'Yes' WHERE user_id = $id"; 

     $result_facebook = mysqli_query($connection, $sql_facebook); 
     $row_facebook = mysqli_fetch_array($result_facebook); 

     } elseif(!isset($_POST['check-facebook']) && $facebook_check == "Yes") { 
     $sql_facebook2 = "UPDATE XXXXXX SET fb_auto_post = 'No' WHERE user_id = $id"; 

     $result_facebook2 = mysqli_query($connection, $sql_facebook2); 
     $row_facebook2 = mysqli_fetch_array($result_facebook2); 
     } 

     // CHECK WEEKLY SCHEDULE 
     if(isset($_POST['check-weekly']) && $schedule_check == "No"){ 
     $sql_weekly = "UPDATE XXXXXXX SET weekly_email = 'Yes' WHERE user_id = $id"; 

     $result_weekly = mysqli_query($connection, $sql_weekly); 
     $row_weekly = mysqli_fetch_array($result_weekly); 

     } elseif(!isset($_POST['check-weekly']) && $schedule_check == "Yes") { 
     $sql_weekly2 = "UPDATE foodtruck_user SET weekly_email = 'No' WHERE user_id = $id"; 

     $result_weekly2 = mysqli_query($connection, $sql_weekly2); 
     $row_weekly2 = mysqli_fetch_array($result_weekly2); 
     } 

     // REFRESH PAGE 
     echo "<meta http-equiv='refresh' content='0'>"; 
     } 

    ?> 

<form method="POST" action="XXXXXXXXX"> 

<label class="switch"> 
    <input type="checkbox" name="check-twitter" <?php echo $twitter_check == 'Yes'?'checked':''; ?>> 
    <div class="slider"></div> 
</label><br/> 

<label class="switch"> 
    <input type="checkbox" name="check-facebook" <?php echo $facebook_check == 'Yes'?'checked':''; ?>> 
    <div class="slider"></div> 
</label><br/> 

<label class="switch"> 
    <input type="checkbox" name="check-weekly" <?php echo $schedule_check == 'Yes'?'checked':''; ?>> 
    <div class="slider"></div> 
</label><br/> 

<input class="button" type="submit" name="submit" value="Save" /> 

</form> 
0
<?php 
// retrieve checkbox value from db 
$check = /* db result (true|false) */; 
?> 

<label class="switch"> 
    <input type="checkbox" name="check-twitter" <?=$check && 'checked' ?> /> 
    <div class="slider"></div> 
</label> 

<?php 
if($_POST['check-twitter']) { 
// sql update checked 
} else { 
// sql update unchecked 
} 
?> 

<input class="button" type="submit" value="Save"/> 
  • 如果只有2個可能的值,如果... ELSE應該被使用。
  • PHP代碼必須包含在PHP標籤中。
  • $ _POST變量可以按原樣使用,無需重新分配。