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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化