如何在php中显示mysql的自动增量数据

时间:2017-06-25 07:37:26

标签: php mysql

在我的大学数据库项目中,我的SQL中有一个自动增量字段滚动号。我想要的是,当新的录取发生并插入学生记录时,它会在同一页面上显示所有发现,包括卷号。然而,尽管我付出了最大努力,但它在卷号中返回0。

以下是代码:

<?php

$con = mysqli_connect("localhost", "root", "") or die("conection error");
mysqli_select_db($con, "hamdard university") or die("dbase error");

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

    $r      = "SELECT RollNo FROM admission_form";
    $result = mysqli_query($con, $r);
    if (mysqli_query($con, $r)) {
        $last_id = mysqli_insert_id($con);
    }


    $n = $_POST['txtname'];
    $f = $_POST['txtfac'];
    $s = $_POST['txtsem'];

    $sql = "insert into admission_form(name,faculty,semester)values                ('$n','$f','$s')";
    mysqli_query($con, $sql);



    echo "<table border=1>
            <th>RollNo</th>
            <th>Name</th>
            <th>Faculty</th>
            <th>Semester</th>";



    echo "<tr>";

    echo "<td>";
    echo $last_id;
    echo "</td>";

    echo "<td>";
    echo $n;
    echo "</td>";

    echo "<td>";
    echo $f;
    echo "</td>";

    echo "<td>";
    echo $s;
    echo "</td>";

    echo "<br>";


}
?>

<html>

<head></head>

<body>
    <form name="f1" action="" method="POST">
        RollNo:
        <input type="text" name="txtroll" readonly> Name:
        <input type="text" name="txtname"> Faculty:
        <input type="text" name="txtfac"> Semester:
        <input type="text" name="txtsem">
        <input type="submit" value="done" name="subbtn">
    </form>
</body>

</html>

4 个答案:

答案 0 :(得分:1)

INSERT查询后需要获取$ last_id

$sql="insert into admission_form(name,faculty,semester)values ...
mysqli_query($con,$sql);
$last_id = mysqli_insert_id($con);

答案 1 :(得分:0)

您错误放置了插入查询。将其更改为:

<?php

$con=mysqli_connect("localhost","root","")or die("conection error");
mysqli_select_db($con,"hamdard university")or die("dbase error");

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

 $sql="insert into admission_form(name,faculty,semester)values                ('$n','$f','$s')";

if (mysqli_query($con, $sql)) 
{
$last_id = mysqli_insert_id($con);
}


$n=$_POST['txtname'];
$f=$_POST['txtfac'];
$s=$_POST['txtsem'];



$r="SELECT RollNo FROM admission_form";
$result=mysqli_query($con, $r);


echo        "<table border=1>
        <th>RollNo</th>
        <th>Name</th>
        <th>Faculty</th>
        <th>Semester</th>";



    echo "<tr>";

    echo "<td>";
    echo $last_id;
    echo "</td>";

    echo "<td>";
    echo $n;
    echo "</td>";

    echo "<td>";
    echo $f;
    echo "</td>";

    echo "<td>";
    echo $s;
    echo "</td>";   

    echo "<br>";


}
?>

<html>
<head></head>
<body>

<form name="f1" action="" method="POST">

RollNo:<input type="text" name="txtroll" readonly>
Name:<input type="text" name="txtname">
Faculty:<input type="text" name="txtfac">
Semester:<input type="text" name="txtsem">
<input type="submit" value="done" name="subbtn">

</form>
</body>
</html>

答案 2 :(得分:0)

试试这个:

$mysqli = new mysqli(SQLI_SERVER, MYSQLI_USER, MYSQLI_PWD, MYSQLI_DBNAME);
if ($result = $mysqli->query("INSERT INTO admission_form(name, facility,semester) VALUES..) {
    echo 'The ID is: '.$mysqli->insert_id;
}

答案 3 :(得分:0)

您在此页面中遇到了一些问题。

  1. 如前所述,INSER查询必须在select之前。
  2. 如果您执行“SELECT RollNo FROM admission_form”,您将在admission_form中获得所有现有的RollNo,而不是最新的。如果这是一个带有自动增量的int(接缝为),你应该“SELECT max(RollNo)FROM admission_form”。
  3. 您不需要执行上述选择,因为mysqli_insert_id会获取上次查询中插入的ID。
  4. 您可能无法在表格中看到结果,因为它位于HTML标题上,但它应位于页面正文中。
  5. 您创建的表格未“关闭”。
  6. 你应该好好接受下面的代码。

    <HTML>
    <HEAD></HEAD>
    <BODY>
    <?php
    $con=mysqli_connect("localhost","root","")or die("conection error");
    mysqli_select_db($con,"hamdard university")or die("dbase error");
    
    if(isset($_POST['subbtn']))
    {
      $n=$_POST['txtname'];
      $f=$_POST['txtfac'];
      $s=$_POST['txtsem'];
    
      $sql="insert into admission_form(name,faculty,semester) values                ('$n','$f','$s')";
    
      if (mysqli_query($con, $r)) 
      {
        $last_id = mysqli_insert_id($con);
      }
    
    echo        "<table border=\"1\"><th>RollNo</th> <th>Name</th> <th>Faculty</th> <th>Semester</th>";
    
        echo "<tr>";
    
        echo "<td>";
        echo $last_id;
        echo "</td>";
    
        echo "<td>";
        echo $n;
        echo "</td>";
    
        echo "<td>";
        echo $f;
        echo "</td>";
    
        echo "<td>";
        echo $s;
        echo "</td>";   
    
        echo "</tr></table>";
    
    
    }
    ?>
    
    <form name="f1" action="" method="POST">
    
    RollNo:<input type="text" name="txtroll" readonly>
    Name:<input type="text" name="txtname">
    Faculty:<input type="text" name="txtfac">
    Semester:<input type="text" name="txtsem">
    <input type="submit" value="done" name="subbtn">
    
    </form>
    </body>
    </html>