2017-04-06 57 views
0
if((isset($_POST['page_id'])) && (isset($_POST['page_title'])) && (isset($_POST['page_file_name']))) { 
    $_POST['page_id']; 
    $page_id = $_POST['page_id']; 
    $page_title = $_POST['page_title']; 
    $page_file_name = $_POST['page_file_name'].".txt"; 

    //to check if id or page already exists in database or not 
    $sql_page_check = "SELECT * FROM page"; 
    $sql_page_check_result = mysqli_query($conn,$sql_page_check); 
    if($sql_page_check_result == true){ 
     if(mysqli_num_rows($sql_page_check_result)>0){ 
      $pages_present = mysqli_num_rows($sql_page_check_result); 

      while($page = mysqli_fetch_assoc($sql_page_check_result)){ 
       if(($page['page_id']==$page_id) || ($page['page_title']==$page_title) || ($page['page_file_name']==$page_file_name)){ 
        echo "Page Id ($page_id) /$page_title/$page_file_name already exists in database, please check and try again"; 
        break1; 
       } 
       elseif(($page_id=="") || ($page_title=="") || ($page_link=="")){ 
        echo ("<b style='color:red;'>Please fill all fields.</b>"); 
        break; 
       } 

       else{ 
        if(!file_exists("../pages/".$page_file_name)){ 
         fopen("../pages/".$page_file_name,"w"); 

         $sql_add_menu = "INSERT INTO page VALUES('$page_id','$page_title','$page_file_name')"; 

         $sql_add_menu_result = mysqli_query($conn,$sql_add_menu); 

         if($sql_add_menu_result == true){ 
          echo"<b style='color:green;'>$page_title Page Added</b>"; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
include "footer.php"; 

問題是每個錯誤消息必須顯示在循環內並顯示多次,如果我使用break,die,退出footer.php底部不顯示出來,還有一部分不起作用!問題與在PHP中的循環斷開

+0

'break1'無效。這是在原始頁面還是複製錯誤? – Barmar

+0

@Barmar ya它只是break1 [只是測試,如果萬一它的工作],通過互聯網搜索解決方案,我發現我們也可以使用break1 break2也取決於有多少循環打破。 –

+0

我想那裏需要有一個空格之前的數字,我猜不是。但是沒有理由使用這個數字,除非你需要從內部循環中突破外部循環。 – Barmar

回答

0

你不需要循環。只需執行一個使用WHERE子句的查詢來查看是否有任何輸入已經在數據庫中。並使用準備好的查詢來防止SQL注入。

在查詢之前,您還應該檢查字段是否空白。

if(($page_id=="") || ($page_title=="") || ($page_link=="")){ 
    echo ("<b style='color:red;'>Please fill all fields.</b>"); 
} else { 
    $sql_page_check = "SELECT 1 FROM page WHERE page_id = ? OR page_title = ? OR page_file_name = ?"; 
    $stmt_page_check = mysqli_prepare($conn, $sql_page_check); 
    mysqli_stmt_bind_param($stmt_page_check, "iss", $page_id, $page_title, $page_file_name); 
    mysqli_stmt_execute($stmt_page_check); 
    if (mysql_stmt_num_rows($stmt_page_check) != 0) { 
     echo "Page Id ($page_id) /$page_title/$page_file_name already exists in database, please check and try again"; 
    } else { 
     if (!file_exists ("../pages/".$page_file_name) { 
      fopen("../pages/".$page_file_name,"w"); 
     } 
     $sql_add_menu = "INSERT INTO page VALUES(?, ?, ?)"; 
     $stmt_add_menu = mysqli_prepare($conn, $sql_add_menu); 
     mysql_stmt_bind_param($stmt_add_menu, "iss", $page_id, $page_title, $page_file_name); 
     $sql_add_menu_result = mysqli_stmt_execute($stmt_add_menu); 
     if($sql_add_menu_result){ 
      echo "<b style='color:green;'>$page_title Page Added</b>"; 
     } 
    } 
} 
+0

之後檢查這個變量,我們可以使用它,但是必須有解決方案來完成工作。在我的代碼中使用break。 –

+0

如果使用循環,則必須等到循環結束才能插入新行。您需要設置一個變量,指出是否找到匹配的行,並在循環之後檢查該行。有關搜索數組的類似問題,請參閱http://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882。 – Barmar