Pagaination hep

Hello Guys,

Im having problems with my code when i add a new record to my mysqli database its svaing the record but isnt displaying the new one it then duplicates the 2nd newest record can anyone help please?.

Its only showing 38 records when it should display 39 as thats whats in the database

<?php include('styles/top.php'); ?>

     <div id="left">
         
          
        <?php
        
        if ($user_level !=1){
        } else {
    
            include("css/menu.php");
        ?>
        <?php
        }
        ?>
        
         
     </div>
    
     <div id="right">
    
      
    <div id="view_box">
            <ul>
                <li><img src="images/1.png" /></li>
                <!-- <li><img src="pics/2.jpg" /></li> -->
                <!-- <li><img src="pics/3.jpg" /></li> -->
            </ul>
        </div>
        
        <div id="button">
            <ul>
                <!-- <li><button class="button" scroll_value="0">*</button></li> -- >
                <!-- <li><button class="button" scroll_value="600">*</button></li> -->
                <!-- <li><button class="button" scroll_value="1200">*</button></li> -->
            </ul>
        </div>
        <hr />
    
        <?php
        
        if ($user_level !=1){
            echo "No Access Allowed Admin Only ";
        } else {
          
        
        if(isset($_GET['page'])){
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        
        if($page == '' || $page == 1){
            $page1 = 0;
        } else {
            $page1 = ($page*4)-4;
        }
        
        $sql = 'SELECT * FROM customers LIMIT '.$page1.', 4';
        $data = $con->query($sql);
        
       ?>

        <h3>CURRENT CUSTOMERS</h3>
 
        
        		<table border=1">
				<tr>
					<th>CUSTOMER NAME </th>
					<th>ACCOUNT STATUS</th>
					<th>UPDATE</th>
					<th>DELETE</th>
				</tr>
                
                <tr>
						<td>&nbsp;</td>
						<td></td>
						<td></td>
						<td></td>
					</tr>

            <?php
                
    
               
                
                while ($myrow = mysqli_fetch_array($data))
                {
                   
                    echo "<TR>
                    
                        <TD>".$myrow["customer_name"]."</TD>
                        <TD>".$myrow["type"]."</TD>";
                        
    				    echo "<TD>
    				    
    				   <a href=\"update_edit_customer.php?id=".$myrow['id']."\">UPDATE</a>
    				    
    				    </TD>";
    				
    					 echo "<TD>
    					 
    					 <a href=\"delete-customer.php?id=".$myrow['id']."\">DELETE</a>
    					 
    					 </TD>";
    				
    					
                    echo "</TR>";
                    
                }
                
                
                ?>
                
                
                   <tr>
						<td>&nbsp;</td>
						<td></td>
						<td></td>
                        <td></td>
					</tr>
                
               
              			
			</table>
            
            
            <br />  
       
       <?php
      
        $sql = "SELECT * FROM customers";
        $data = $con->query($sql);
        $records = $data->num_rows;
        $records_pages = $records/4;
        $records_pages = ceil($records_pages);
       
        $prev = $page-1;
        $next = $page+1;
        
        echo '<ul class="paganation">';
        
        if($prev >= 5){
            echo '<li><a href="?page=1">First Page</a></li>';
        }
        
        if($prev >=1){
            echo '<li><a href="?page='.$prev.'">Previous</a></li>';
        }
        
        
        if($records_pages >= 2 ){
            for($r=1; $r <= $records_pages; $r++){
                $active = $r == $page ? 'class="active"' : '';
                echo '<li><a href="?page='.$r.'" '.$active.'>'.$r.'</a></li>';
            }
        }
        
        
        if($next <= $records_pages && $records_pages >= 2){
            echo '<li><a href="?page='.$next.'">Next</a></li>';
        }
        
        if($page != $records_pages && $records_pages >= 5){
            echo '<li><a href="?page='.$records_pages.'">Last Page</a></li>';
        }
        
        echo '</ul>';

 
        ?>
        
        <?php
        }
        ?>
        
        
       
    </div>

<?php include('styles/bottom.php'); ?>

The code is displaying exactly what the query matches. Any chance you have 2 tables or 2 databases or 2 different database connections and the database and/or table you are inserting the data into isn’t the same database and/or table you are querying and display the data from?

Beyond this, you would need to post all the code needed for the insert and display operations, less any database connection credentials, to get any specific help.

Some points that have already been posted elsehwere about your code -

  1. This page (operation) requires a logged in user with administrator level permissions. You should perform that check once, near the top of the code and prevent access to the whole page if the user doesn’t have permission to access the page. This falls under Don’t Repeat Yourself (DRY.)
  2. Don’t escape double-quotes inside a double-quoted php string, simply use single-quotes inside the string.
  3. Don’t query for all the columns and all the rows of data to get a count of the number of rows. Use a SELECT COUNT(*) … query, then fetch the COUNT() value.

And here’s a new one for the posted code, the hard-code 4 (number of rows per page) should be defined in a variable, so that you can change the value at a single point by simply changing the variable assignment.

I have one table called customers

The insert code is only inserting a single record

If you cannot troubleshoot this yourself, you will need to post the stated code and a .sql dump of the database. There are just too many possibilities that could cause this symptom.

This is my insert record code

<?php include('styles/top.php'); ?>

     <div id="left">
         
          
        <?php
        
        if ($user_level !=1){
        } else {
    
        include("css/menu.php");

        
        ?>
        
        

 
        <?php
        }
        ?>
        
         
     </div>
    
     <div id="right">
    
      
    <div id="view_box">
            <ul>
                <li><img src="images/1.png" /></li>
                <!-- <li><img src="pics/2.jpg" /></li> -->
                <!-- <li><img src="pics/3.jpg" /></li> -->
            </ul>
        </div>
        
        <div id="button">
            <ul>
                <!-- <li><button class="button" scroll_value="0">*</button></li> -- >
                <!-- <li><button class="button" scroll_value="600">*</button></li> -->
                <!-- <li><button class="button" scroll_value="1200">*</button></li> -->
            </ul>
        </div>
        <hr />
    
        <?php
        
        if ($user_level !=1){
            echo "No Access Allowed Admin Only ";
        } else {

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

        $customer_name = $_POST['customer_name'];
        $username = $_POST['username'];
        $password = md5($_POST['password']);

        $sql = "insert into customers(customer_name, username, password, user_level, type, date) values('$customer_name', '$username', '$password', '1', 'Active', '')";
        
        
        if ($con->query($sql) === TRUE) {
             echo'Your New Cusomter has been added to the database';
        }
        
        $con->close();    
        
        }
            
        ?>
        
        <form action="" method="post">
        <table>
          
            <tr>
                <td>Customers Name:</td>
                <td><input type="text" name="customer_name"></td>
            </tr>
            
            <tr>
                <td>Customers Username:</td>
                <td><input type="text" name="username"></td>
            </tr>
            
            <tr>
                <td>Customers Password:</td>
                <td><input type="Password" name="password"></td>
            </tr>
    
            <tr>
                <td></td>
                <td><input type="submit" class="button" value="Save New Customer" name="add"></td>
            </tr>
            
        </table>
        </form>
        


        <?php
        }
        ?>
        
        
       
    </div>

<?php include('styles/bottom.php'); ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service