簡體   English   中英

如何根據帶有數據庫中最后一個ID的下拉選擇自動生成ID號

[英]how to generate automatically id number based on the drop down selection with last id from the database

我有一種形式,其中有一個稱為索引的字段。 我要做的是自動生成索引號,如下所示。

ex: option1_01 , option2_01 , option1_02 .. etc

option1基於下拉選擇。 _01來自數據庫,使用表option1的最后一個ID號。

這是我的form.php

 <body>
<form id="" method="post" action="send.php">
<select name="name" id="name">
    <option value="">please select</option>
    <option value="option1">option1</option>
    <option value="option2">option2</option>
</select><br>
<label for="index">Index</label>
    <input type="text" id="index" name="index" readonly="readonly" value=""/><br>
<label for="fname">fname</label>
    <input type="text" name="fname"/><br>
<label for="lname">lname</label>
    <input type="text" name="lname"/><br>
<label for="age">age</label>
    <input type="text" name="age"/>
<button name="submit">sumit</button>
</form> 
</body>

我也有兩個表來插入數據。 當我們選擇option1 ,其余細節應進入option1表。

db.php

<?php
           $servername = "localhost";
           $username = "root";
           $password = "";
           $db = "testing01";

 // Create connection
           $conn = mysqli_connect($servername, $username, $password,$db);

// Check connection
            if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
            }
            echo "Connected successfully <br/>";
?>  

這是我的jQuery代碼

$(document).ready(function() {  

$("#name option").filter(function() {    
      return $(this).val() == $("#index").val(); 
    }).attr('selected', true);  

var index1 = $("#index").val();

$("#name").on("change", function() {
    $.ajax({
        url:'table.php',
        type:'post',
        data:{index1:index2},
        success:function(){
         $("#index").val($(this).find("option:selected").attr("value"));
        }
    });
});
}); 

table.php

<?php   
             $index2 = $_POST['index2'];
             include("db.php");

                $sql = "SELECT MAX(id) FROM ".$index2." ";
                $res = mysqli_query($conn,$sql);
                $get = mysqli_fetch_array($res); 
                $next_id = $get['MAX(id)'] + 1;

        }
?>

send.php

<?php
  if(isset($_POST['submit'])){
    $option=$_POST['name'];
    $index=$_POST['index'];
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $age=$_POST['age'];


    include('db.php');

    if($option=="option1"){
        $result1=mysqli_query($conn,"INSERT INTO option1(options,fname,lname,age) VALUES('$option','$fname','$lname','$age');");
      if($result1){
      echo "data inserted to option1 table";
    }
    }

    if($option=="option2"){
      $result2=mysqli_query($conn,"INSERT INTO option2(options,fname,lname,age) VALUES('$option','$fname','$lname','$age');");
      if($result2){
      echo "data inserted to option2 table";
    }
    }
    }
?>

我認為$ ajax代碼段中存在問題。 它將index2變量傳遞到table.php 但是我無法找回$next_id因為我是jquery新手。 有人幫我解決這個問題嗎?

在查詢中使用別名MAX(id) as maxId來獲取記錄。

Table.php

<?php   
             $index2 = $_POST['index2'];
             include("db.php");

                $sql = "SELECT MAX(id) as maxId FROM ".$index2." ";
                $res = mysqli_query($conn,$sql);
                $get = mysqli_fetch_array($res); 
                $next_id = $get['maxId'] + 1;

        }
?>

終於我解決了自己

我的jquery代碼段應按此處更改

$(document).ready(function(){                                       
        $('#name').on('change', function() {
           $('#index').val($(this).find('option:selected').attr('value'));

           var index1 = $('#index').val();

        $.ajax({
             url:'table.php',
             type:'post',
             data:{index1:index2},
             dataType:'json',
             success:function(data){

                var next_id = data;
                $("#index").val(index1 + "_" + next_id);
                //alert('success');
                 }
           });


        });
     }); 

$sql = "SELECT MAX(id) AS max_id FROM ".$index2." ";
                $res = mysqli_query($conn,$sql);
                $get = mysqli_fetch_array($res); 
                $next_id = $get['max_id'] + 1;
                echo $next_id;

您的ajax腳本沒有結果。

嘗試這個

$("#name").on("change", function() {
$.ajax({
    url:'table.php',
    type:'post',
    data:{index1:index2},
    success:function(result){

     //result = $next_id from your table.php script...

     $("#index").val($(this).find("option:selected").attr("value"));
    }
 });
});

並在您的插入腳本中:

            ......
            $res = mysqli_query($conn,$sql);
            $get = mysqli_fetch_array($res); 
            $next_id = $get['maxId'] + 1;
            echo $next_id;

$ next_id將在結果變量中傳遞回ajax腳本。 (實際上,您可以隨便調用此名稱...)

然后您可以對結果執行所需操作,因為它將= $ next_id

編輯:

另外,我在您的jquery代碼中沒有看到index2的定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM