如何将索引重置为0

时间:2017-02-11 12:22:18

标签: php mysql

我应该进行“检查”并且应该有(Index,id,....),但索引应该在100之后重置,但id必须是auto_incremented。 这是我的代码我做了什么。我没有得到如何将索引重置为0.我甚至无法显示索引值,每次添加时都会显示0。

<?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;
    }
?>

这是我的输出:http://imgur.com/GOuCcBU

1 个答案:

答案 0 :(得分:0)

请查看你的phpmyadmin页面中有auto_increment选项,你只需要为id字段检查两个字段检查自动增量,对于索引你只需要获取它并将其保存到另一个名为number_index的字段中的db(因为索引是保留的字)。

通过执行类似这样的操作将您的数据库重置为O是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\';';
  }

和html这样的东西

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