Php 从刚刚插入的表行获取的ID
我有免费的文本框TP_ID,Deny,Level 当我输入Deny和Level的值并单击submit时,它们被存储到数据库中,并且TP_ID是自动递增的Php 从刚刚插入的表行获取的ID,php,mysql,auto-increment,Php,Mysql,Auto Increment,我有免费的文本框TP_ID,Deny,Level 当我输入Deny和Level的值并单击submit时,它们被存储到数据库中,并且TP_ID是自动递增的 <form name="Permit" id="Permit" action="<?php echo JURI::current(); ?>" method="post"> <input type="text" name="TPID" value=""> <br>
<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=$db->insert\u id代码>
假设它使用的是mysqli
使用
$db->insertid()
见:
有关更多信息。这应该可以:
SELECT LAST_INSERT_ID();
资料来源:
您可以使用MySQLLAST\u INSERT\u 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)
你是说这个$db->insertid();?请注意,在本例中,“停止/启动”(“/”)是不必要的。如果您使用的是DBO
,为什么不使用参数化查询?为什么要让自己容易受到注射攻击呢。它不是insertid(),而是insert_id(),您在哪里发现OP正在使用Joomla?$db=&JFactory::getDBO();感谢各位,我曾经使用过$db->insertid();如何将其存储到其他表中?谢谢,但一旦我获得最后一个Id,如何将其插入其他表中?