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

the issue is every error message which has to be displayed is within the loop and gets displayed many times , if i use break,die,exit the footer.php at bottom doesn't shows up and also else part doesn't work !

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `break1` is not valid. Is that in the original page or a copying error? – Barmar Apr 06 '17 at 22:46
  • @Barmar ya its just break1[just tested if in case it works], searched for solution over internet and i found we can use break1 break2 also depending upon how many loops to break. –  Apr 06 '17 at 22:56
  • I thought there needed to be a space before the number, I guess not. But there's no reason to use the number unless you need to break out of an outer loop from an inner one. – Barmar Apr 06 '17 at 23:04
  • no space is not mandatory , and it was just a hit and try method :p –  Apr 06 '17 at 23:09
  • In this case the answer from @Barmar is the preferred one. However, if you have this problem again, you can set a variable (like `$is_error`) to `true` and check this variable after the loop – Aloso Apr 06 '17 at 23:49

1 Answers1

0

You don't need a loop. Just do a single query that uses a WHERE clause to see if any of the inputs are already in the database. And use a prepared query to prevent SQL injection.

You should also check if the fields are blank before doing the query.

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>";
        }
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • ya that we can use but there must be solution for getting work done the way i used in my code using break. –  Apr 06 '17 at 23:01
  • If you use a loop, you have to wait until the end of the loop to insert the new row. You need to set a variable that says whether a matching row was found or not, and check that after the loop. See http://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882 for a similar issue with searching arrays. – Barmar Apr 06 '17 at 23:03