kputri
kputri

Reputation: 63

how to make ascending id in php

this code not asceding id. we have id 1, 2, 3, 15. when adding next record is read id 16. so how to solve this problem ? id must 1,2,3,4,5,etc

    <?php
include"class_koneksi.php";
$db=new database();
$db->konek();


    if(empty($_GET['aksi'])){

        $daftar=$db->tampilData();
            echo"<table border=1>
                <tr>
                    <td>Id</td>
                    <td>Nama</td>
                    <td>Email</td>
                    <td>Pesan</td>
                    <td>Edit</td>
                    <td>Hapus</td>
                </tr>";
        foreach($daftar as $data){
            echo" <tr>
                    <td>".$data['id']."</td>
                    <td>".$data['nama']."</td>
                    <td>".$data['email']."</td>
                    <td>".$data['pesan']."</td>
                    <td><a href='?aksi=edit&id=$data[id]'>edit</a></td>
                    <td><a href='?aksi=hapus_data&id=$data[id]'>Hapus</a></td>
                </tr>";
        }
        echo"</table> <br> <a href='?aksi=tambah'>TAMBAH</a>";


    }elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
        echo"<br>
        <form method=POST action='?aksi=tambah_data'>
            <table>

                <tr><td>Nama</td><td><input type=text name='nama'></td></tr>
                <tr><td>Email</td><td><input type=text name='email'></td></tr>
                <tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
                <tr><td></td><td><input type=submit value='simpan'></td></tr>
            </table>
        </form>
        ";

    }elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
        $id= isset($_POST['id']);
        $nama=$_POST['nama'];
        $email=$_POST['email'];
        $pesan=$_POST['pesan'];
        $db->tambahData($id,$nama,$email,$pesan);

    }elseif(isset($_GET['aksi']) && $_GET['aksi']=='edit'){
        $id=$_GET['id'];
        echo"<br>
            <form method=POST action='?aksi=update_data'>
                <table>
                    <tr><td>Id</td><td><input type=text name='id' value='".$db->bacaData('id',$id)."'></td></tr>
                    <tr><td>Nama</td><td><input type=text name='nama' value='".$db->bacaData('nama',$id)."'></td></tr>
                    <tr><td>Email</td><td><input type=text name='email' value='".$db->bacaData('email',$id)."'></td></tr>
                    <tr><td>Pesan</td><td><input type=text name='pesan' value='".$db->bacaData('pesan',$id)."'></td></tr>
                    <tr><td></td><td><input type=submit value='simpan'></td></tr>
                </table>
            <input type='hidden' name='id' value='".$db->bacaData('id',$id)."'>
            </form>
        ";

    }elseif(isset($_GET['aksi']) && $_GET['aksi']=='update_data'){
        $id=$_POST['id'];
        $nama=$_POST['nama'];
        $email=$_POST['email'];
        $pesan=$_POST['pesan'];
        $db->updateData($id,$nama,$email,$pesan);

    }elseif(isset($_GET['aksi']) && $_GET['aksi']=='hapus_data'){
        $id=$_GET['id'];
        $db->hapusData($id);
    }

?> 

can you tell me where this code wrong? can you help me solve this problem? thanks

Upvotes: 0

Views: 446

Answers (3)

Soumik Sur
Soumik Sur

Reputation: 167

First remove auto increment from id in the respective database table you are having this issue. Then Replace this code section with your code.

.
.
.
elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
        $daftar=$db->tampilData();
        $id=count($daftar)+1;
        if(count($daftar)>0){
           $temp = 1;
           forech($daftar as $row){
               if($row['id']!=$temp++){
                   $id=$temp; break;
               }

           }
        }
        echo"<br>
        <form method=POST action='?aksi=tambah_data'>
            <table>

                <tr><td>Nama</td><td><input type=text name='nama'><input type="hidden" name='id' value="<?php echo $id ?>"></td></tr>
                <tr><td>Email</td><td><input type=text name='email'></td></tr>
                <tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
                <tr><td></td><td><input type=submit value='simpan'></td></tr>
            </table>
        </form>
        ";

    }elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
        $id= $_POST['id'];
        $nama=$_POST['nama'];
        $email=$_POST['email'];
        $pesan=$_POST['pesan'];
        $db->tambahData($id,$nama,$email,$pesan);

    }
.
.
.

I hope this solves your requirement..

Upvotes: 0

Soumik Sur
Soumik Sur

Reputation: 167

Remove the auto increment property of the table which is showing 16.

Before inserting a new row in that table write a select query.

$sql = "select * from `table_name`";
$result = mysql_query($sql);

$id=count($result)+1;
if(count($result)>0){
   $temp = 1;
   while($row = mysql_fetch_assoc($result)){
       if($row['id']!=$temp++){
           $id=$temp; break;
       }

   }
}

$sql = "insert into `table_name` (`id`,`..`,`..`) values('"$id.."','..','..')";
mysql_query($sql);

I hope this will solve your query...

Upvotes: 2

Vishal
Vishal

Reputation: 553

It is showing id as 16 becouse in table properties Id is set as auto increment

Upvotes: 0

Related Questions