从我刚插入的表行中获取ID

时间:2014-03-14 09:36:40

标签: php mysql auto-increment

我有免费的文本框TP_ID,Deny,Level

当我输入Deny和Level的值时,我点击提交,它们存储在数据库中,TP_ID是Auto_Increment。

<form name="Permit" id="Permit" action="<?php echo JURI::current(); ?>" method="post">
        <input type="text" name="TPID" value=""> <br>
        <input type="text" name="Permit_or_Deny" value=""> <br>
        <input type="text" name="Level" value=""> <br>


        <p>input id="submit" name="submit" type="submit" value="save" /></p>

     </form>
<?php
    if( (isset($_POST['Permit_or_Deny'])) || (isset($_POST['Level']))|| (isset($_POST['TPID'])) ) 
        {

        $Permit_or_Deny = $_POST['Permit_or_Deny'];
        $Level= $_POST['Level'];
        $TPID= $_POST['TPID'];
        $db =& JFactory::getDBO();   
        $query = "INSERT INTO tp_newedit (TP_ID, Permit_or_Deny, Level) 
        VALUES ('".$TPID."','".$Permit_or_Deny."','".$Level."');";
        $db->setQuery( $query );
        $db->query();
        echo "<script>window.close();</script>";
        } else

    ?>

有没有办法检索最后创建的ID并将其显示在文本框中?

4 个答案:

答案 0 :(得分:0)

这不起作用吗? $id = $db->insert_id;

假设它使用mysqli

http://www.php.net/manual/en/mysqli.insert-id.php

答案 1 :(得分:0)

使用

$db->insertid()

请参阅: http://docs.joomla.org/API17:JDatabase::insertid 了解更多信息。

答案 2 :(得分:0)

答案 3 :(得分:0)

您可以使用MySQL LAST_INSERT_ID()功能。这将返回给定会话的最后插入ID。

以下是一个例子:

mysql> INSERT INTO cities VALUES (NULL, 'Banglore','India',42789);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM cities;
+----+------------+---------+------------+
| id | city       | country | population |
+----+------------+---------+------------+
|  5 | Paris      | France  |   11836970 |
|  6 | Nantes     | France  |     283025 |
|  7 | London     | UK      |    7556900 |
|  8 | Manchester | UK      |     464200 |
|  9 | Hull       | UK      |     258700 |
| 11 | Banglore   | India   |      42789 |
+----+------------+---------+------------+
6 rows in set (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|               11 |
+------------------+
1 row in set (0.00 sec)