<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Gravity Support Forums Topic: Insert form: $wpdb-&#62;insert_id vs. $wpdb-&#62;get_var(&#34;SELECT LAST_INSERT_ID();&#34;);</title>
		<link>https://legacy.forums.gravityhelp.com/topic/insert-form-wpdb-insert_id-vs-wpdb-get_varselect-last_insert_id</link>
		<description>Gravity Support Forums Topic: Insert form: $wpdb-&gt;insert_id vs. $wpdb-&gt;get_var(&quot;SELECT LAST_INSERT_ID();&quot;);</description>
		<language>en-US</language>
		<pubDate>Fri, 03 Apr 2026 22:57:38 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>https://legacy.forums.gravityhelp.com/search.php</link>
		</textInput>
		<atom:link href="https://legacy.forums.gravityhelp.com/rss/topic/insert-form-wpdb-insert_id-vs-wpdb-get_varselect-last_insert_id" rel="self" type="application/rss+xml" />

		<item>
			<title>MMC on "Insert form: $wpdb-&#62;insert_id vs. $wpdb-&#62;get_var(&#34;SELECT LAST_INSERT_ID();&#34;);"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/insert-form-wpdb-insert_id-vs-wpdb-get_varselect-last_insert_id#post-9636</link>
			<pubDate>Thu, 09 Sep 2010 17:54:09 +0000</pubDate>
			<dc:creator>MMC</dc:creator>
			<guid isPermaLink="false">9636@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Great :)&#60;/p&#62;
&#60;p&#62;Just wanted to point out that there's two other places in RGFormsModel where the LAST_INSERT_ID query is used, and I didn't find it in any other files.  &#60;/p&#62;
&#60;p&#62;Specifically lines 361, 577, and 1027 in forms_model.php (version 1.3.11)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Alex Cancado on "Insert form: $wpdb-&#62;insert_id vs. $wpdb-&#62;get_var(&#34;SELECT LAST_INSERT_ID();&#34;);"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/insert-form-wpdb-insert_id-vs-wpdb-get_varselect-last_insert_id#post-9623</link>
			<pubDate>Thu, 09 Sep 2010 14:35:31 +0000</pubDate>
			<dc:creator>Alex Cancado</dc:creator>
			<guid isPermaLink="false">9623@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Using $wpdb-&#38;gt;insert_id does seem to be the better solution. I will have that changed in the next release. There isn't any special reason why the LAST_INSERT_ID() is used, so you can go ahead and make the change on your end in the meantime.&#60;br /&#62;
Thanks for the detailed post.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>MMC on "Insert form: $wpdb-&#62;insert_id vs. $wpdb-&#62;get_var(&#34;SELECT LAST_INSERT_ID();&#34;);"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/insert-form-wpdb-insert_id-vs-wpdb-get_varselect-last_insert_id#post-9618</link>
			<pubDate>Thu, 09 Sep 2010 13:42:38 +0000</pubDate>
			<dc:creator>MMC</dc:creator>
			<guid isPermaLink="false">9618@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Just to expand, I think this is a bug.  From what I could tell, it's because $wpdb-&#38;gt;get_var(&#34;SELECT LAST_INSERT_ID();&#34;); performs a separate query to the database for all connections, so if there's a select, update or a different insert via another db connection it will pick up that insert id (usually 0, because there are so many selects).  Whereas $wpdb sets $wpdb-&#38;gt;insert_id specifically using the current connection resource and immediately after the insert takes place.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>MMC on "Insert form: $wpdb-&#62;insert_id vs. $wpdb-&#62;get_var(&#34;SELECT LAST_INSERT_ID();&#34;);"</title>
			<link>https://legacy.forums.gravityhelp.com/topic/insert-form-wpdb-insert_id-vs-wpdb-get_varselect-last_insert_id#post-9586</link>
			<pubDate>Wed, 08 Sep 2010 16:42:52 +0000</pubDate>
			<dc:creator>MMC</dc:creator>
			<guid isPermaLink="false">9586@https://legacy.forums.gravityhelp.com/</guid>
			<description>&#60;p&#62;Had a weird issue, we're using GF 1.3.11 on WP 2.9.2&#60;/p&#62;
&#60;p&#62;Development server running Ubuntu:&#60;br /&#62;
PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch&#60;br /&#62;
MySQL 5.1.45-log MySQL Community Server (GPL)&#60;/p&#62;
&#60;p&#62;Production server running Debian:&#60;br /&#62;
PHP Version 5.2.6-1+lenny8 with Suhosin-Patch&#60;br /&#62;
MySQL 5.0.51a-24+lenny4-log (Debian) running Debian&#60;/p&#62;
&#60;p&#62;On the dev server GF works fine.  &#60;/p&#62;
&#60;p&#62;On the production server, when creating a new form, the new form data in the rg_form_meta table is saved with form_id=0 so after creating a new form, when you go back to it later it has no fields and the UI shows the default &#34;Untitled Form&#34; box.  Then when you try and create a new form, new forms have the form data that was previously saved to the new form and is now &#34;missing&#34;.  And new forms you try and create give the &#34;A form already exists with that name&#34; error.&#60;/p&#62;
&#60;p&#62;I installed GF on another site running WP 2.8.5 on the same production server that the bugs appeared on, and it works fine.&#60;/p&#62;
&#60;p&#62;I dug around in the code and found in RGFormsModel::insert_form() that the following is being returned:&#60;br /&#62;
return $wpdb-&#38;gt;get_var(&#34;SELECT LAST_INSERT_ID();&#34;);&#60;/p&#62;
&#60;p&#62;On the production server with WP 2.9.2 this is returning 0, and thus saving the form data with ID 0 and causing the whole thing to bug out.  When I change it to use WPDB's insert_id (which stores the return value of mysql_insert_id($wpdb-&#38;gt;dbh) right after an insert) it works.  Like so:&#60;br /&#62;
return $wpdb-&#38;gt;insert_id;&#60;/p&#62;
&#60;p&#62;I've had a hard time reproducing this issue, even with another copy of WP on the same server, so I'm not sure what the exact problem is.  &#60;/p&#62;
&#60;p&#62;However, the fix works flawlessly on both WP installs (the one experiencing the bug and the one that doesn't).  I'm curious why SELECT LAST_INSERT_ID() is used instead of using WPDB's insert_id var, especially since it prevents running two queries to get the same data (WPDB runs a function to get he last insert id and the GF runs a different one to attempt to get the same data). I don't want to inadvertently break something if -- for example -- calling SELECT LAST_INSERT_ID() itself is being used to fix another bug.
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
