duancifu6769 2014-08-21 02:07
浏览 75
已采纳

从php上次执行的mysqli查询中获取结果,并将结果用于另一个查询

I have some AJAX/PHP/SQL stuff happening at the moment.

On one page, you select people you want to put into the database (with checkboxes) jQuery collects the values assigned to the checkbox and stores it in an array. Then I use AJAX to to send the array to a PHP file.

In the PHP file, I want to use that array to insert the value into a table. Which works great.

Once the query is executed, I want to be able to grab the info stored, specifically the ID that is generated to use in another query. I just don't know how to call the last executed query and grab the ID value. This is what I have so far in my PHP file:

$myArray = $_REQUEST['activitiesArray'];
mysqli_query($conn,"INSERT INTO Groups (leader_ID) VALUES (".$myArray[0].")");
printf("%s
", mysqli_info($conn)); //debugging, see if I can pull out anything.

note - This code is very basic. I just want functionality to work before making it dynamic

In the Groups table there are 2 columns, leader_ID and gp_ID (which auto increments). I want to use the last executed query to find out what the value of that gp_ID is so I can use it to update another table. I don't know if this is at all possible. Any insight would be fantastic, cheers.

  • 写回答

1条回答 默认 最新

  • dougou3871 2014-08-21 02:17
    关注

    use mysqli_insert_id($conn) to get the auto-increment ID created by the last INSERT query on the connection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog