简体   繁体   中英

How can I reset index to 0

i should make a "cheque" and there should be (Index,id,....), but index should reset after 100, but id must be auto_incremented. Here is my code what i did. I didnt get how can i reset index to 0. I cant even show the indexs value, it shows 0 everytime when i add something.

<?php
    $host = "localhost";
    $user = "root";
    $password = "";
    $database = "dbcar";

    $number = "";
    $state_number = "";
    $model = "";
    $status = "";
    $conditions = "";
    $date = "";
    $number_index = "1";

    $connect = mysqli_connect($host,$user,$password,$database);

    echo "<h1>Report Log </h1>";
    $sqlget = "SELECT * FROM carserv ORDER BY date DESC LIMIT 1";
    $sqldata = mysqli_query($connect,$sqlget) or die("error");

    echo "<table>";
    echo"<tr>
    <th>INDEX</th>
    <th>ID</th>
    <th>State Number</th>
    <th>Model</th>
    <th>Status</th>
    <th>Condition</th>
    <th>Date</th>
    </tr>";


    while($row = mysqli_fetch_array($sqldata,MYSQLI_ASSOC)){
        echo" <tr><td>";
        echo $row['number_index'];
        echo" </td><td>";
        echo $row['number'];
        echo" </td><td>";
        echo $row['state_number'];
        echo" </td><td>";
        echo $row['model'];
        echo" </td><td>";
        echo $row['status'];
        echo" </td><td>";
        echo $row['conditions'];
        echo" </td><td>";
        echo $row['date'];
        echo" </td></tr>";
    }
    echo "</table>";

    function getPosts(){
        $posts = array();
        $posts[0] = $_POST['state_number'];
        $posts[1] = $_POST['number'];
        $posts[2] = $_POST['model'];
        $posts[3] = $_POST['status'];
        $posts[4] = $_POST['conditions'];
        $posts[6] = $_POST['number_index'];
        return $posts;
    }
?>

and here is my output: http://imgur.com/GOuCcBU

Take look in your phpmyadmin page there is auto_increment option you need to just have two field check auto increment for id field and for index you just fetch it and save it to db in another field with name number_index(because index is a reserved word).

Reset your db to O by doing something like this is your counter_update.php

   if($_POST['index_reset']) {
   $index_reset = $_POST[index_reset];
   mysql_connect("server", "username", "password") or die(mysql_error()); 
   mysql_select_db("database") or die(mysql_error());
   $sql = 'UPDATE counters SET number_index =\'0\' WHERE Page = \'$index_reset\';';
  }

And html side something like this

 $page_reset = "<form id='Reset' action='counter_update.php' method='post'>
<button type='submit' name='index_reset' value='$formPage'>RESET</button>
</form>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM