<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
	<title>jQuery Grid Plugin - jqGrid - Topic: jqGrid php: inserting  and table join</title>
	<link>http://www.trirand.com/blog/?page_id=393/discussion/jqgrid-php-inserting-and-table-join</link>
	<description><![CDATA[Grid plugin]]></description>
	<generator>Simple:Press Version 5.7.5.3</generator>
	<atom:link href="http://www.trirand.com/blog/?page_id=393/discussion/jqgrid-php-inserting-and-table-join/rss" rel="self" type="application/rss+xml" />
        <item>
        	<title>galli2000 on jqGrid php: inserting  and table join</title>
        	<link>http://www.trirand.com/blog/?page_id=393/discussion/jqgrid-php-inserting-and-table-join#p22831</link>
        	<category>Discussion</category>
        	<guid isPermaLink="true">http://www.trirand.com/blog/?page_id=393/discussion/jqgrid-php-inserting-and-table-join#p22831</guid>
        	        	<description><![CDATA[<p>Hello,</p>
<p>While evaluating the possibilities of jqGrid, I have some difficulties for inserting a new row in a table which one column was obtained by a JOIN.</p>
<p>Here is my sample source code:</p>
<p><input type='button' class='sfcodeselect' name='sfselectit6814' value='Select Code' data-codeid='sfcode6814' /></p>
<div class='sfcode' id='sfcode6814'>&#60;?php</p>
<p>ini_set(&#34;display_errors&#34;,&#34;1&#34;);</p>
<p>require_once &#39;common.php&#39;;<br />require_once &#39;jq-config.php&#39;;<br />// include the jqGrid Class<br />require_once &#34;php/jqGrid.php&#34;;<br />// include the PDO driver class<br />require_once &#34;php/jqGridPdo.php&#34;;<br />// Connection to the server<br />$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);<br />// Tell the db that we use utf-8<br />$conn-&#62;query(&#34;SET NAMES utf8&#34;);</p>
<p>// Create the jqGrid instance<br />$grid = new jqGridRender($conn);<br />$grid-&#62;debug = true;<br />// Show the server errors at client<br />$grid-&#62;showError = true;</p>
<p>// Write the SQL Query<br /><span style=&#34;&#34;&#34;&#34;color:&#34;&#34;&#34;>$grid-&#62;SelectCommand = &#39;SELECT component.id, component.type, user.name FROM component,user WHERE component.provider_id = user.id&#39;;<br /></span><br />// set the ouput format to json<br />$grid-&#62;dataType = &#39;json&#39;;<br />// Set the table to where you add the data<br />$grid-&#62;table = &#39;component&#39;;</p>
<p>// Set the url from where we obtain the data<br />$grid-&#62;setUrl(&#39;question.php&#39;);<br />$grid-&#62;cacheCount = true;</p>
<p>// Set grid caption using the option caption<br />$grid-&#62;setGridOptions(array(<br />&#160;&#160;&#160; &#34;caption&#34;=&#62;&#34;join&#34;,<br />&#160;&#160;&#160; &#34;rowNum&#34;=&#62;10,<br />&#160;&#160;&#160; &#34;sortname&#34;=&#62;&#34;id&#34;,<br />&#160;&#160;&#160; &#34;hoverrows&#34;=&#62;true,<br />&#160;&#160;&#160; &#34;rowList&#34;=&#62;array(10,20,50),<br />&#160;&#160;&#160; &#39;shrinkToFit&#39; =&#62; true,<br />&#160;&#160;&#160; &#39;width&#39; =&#62; 700,<br />&#160;&#160;&#160; ));</p>
<p>// Change the col model<br />$model = array(array(NAME =&#62; &#39;id&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LABEL =&#62; &#39;ID&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WIDTH =&#62; 60,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; EDITABLE =&#62; false),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; array(NAME =&#62; &#39;type&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LABEL =&#62; &#39;Article&#39;),<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; array(NAME =&#62; &#39;name&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LABEL =&#62; &#39;Fournisseur&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WIDTH =&#62; 160));<br />$grid-&#62;setColModel($model);</p>
<p>// The primary key should be entered<br />$grid-&#62;setColProperty(&#39;id&#39;, array(&#34;editrules&#34; =&#62; array(&#34;required&#34; =&#62; true)));</p>
<p>// Enable navigator<br />$grid-&#62;navigator = true;</p>
<p>// Use a dropdown for picking a component type<br />$grid-&#62;setSelect(&#39;type&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#39;SELECT id, type FROM component GROUP BY type&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; false);&#160;&#160;&#160;&#160;&#160;&#160;&#160; // do not connect the key value with &#39;type&#39;<br />// Use a dropdown for picking a user name<br />$grid-&#62;setSelect(&#39;name&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#39;SELECT id, name FROM user GROUP BY name&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; false);&#160;&#160;&#160;&#160;&#160;&#160;&#160; // do not connect the key value with &#39;name&#39;</p>
<p>// Enable only adding<br />$grid-&#62;setNavOptions(&#39;navigator&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; array(&#34;excel&#34; =&#62; false,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;add&#34; =&#62; true,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;edit&#34; =&#62; true,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;del&#34; =&#62; true,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;view&#34; =&#62; false,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;search&#34; =&#62; false));<br />// Close the dialog after the record is added<br />$grid-&#62;setNavOptions(&#39;add&#39;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; array(&#34;closeAfterAdd&#34; =&#62; true,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#34;reloadAfterSubmit&#34; =&#62; true)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; );</p>
<p>// Enjoy<br />$grid-&#62;renderGrid(&#39;#question&#39;,&#39;#question_pager&#39;,true, null, null, true,true);<br />$conn = null;<br />?&#62;</p>
</div>
<p>So, to recap, I have a grid that shows a component.id, a component.type and a user.name, and&#160; I want the user to input a component type and a user name, resulting in an INSERT query of a component.type and component.provider_id&#160; into the &#39;component&#39; table.</p>
<p>I don&#39;t know how to have jqGrid make a link between the user.name and the component.provider_id.</p>
<p>I found no hint from the examples.</p>
<p>The error I see on the insert dialog box is the following:</p>
</p>
<p><input type='button' class='sfcodeselect' name='sfselectit7478' value='Select Code' data-codeid='sfcode7478' /></p>
<div class='sfcode' id='sfcode7478'>
<p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mydb`.`component`, CONSTRAINT `component_ibfk_1` FOREIGN KEY (`provider_id`) REFERENCES `user` (`id`))</p>
</div>
<p>and the jqGrid.log contains:</p>
</p>
<p><input type='button' class='sfcodeselect' name='sfselectit8707' value='Select Code' data-codeid='sfcode8707' /></p>
<div class='sfcode' id='sfcode8707'>Array<br />(<br />&#160;&#160;&#160; [0] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [time] =&#62; 2011-04-18 12:23:27<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [query] =&#62; SELECT * FROM component WHERE 1=2<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [data] =&#62; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [types] =&#62; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [fields] =&#62; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [primary] =&#62; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [input] =&#62; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160; [1] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [time] =&#62; 2011-04-18 12:23:27<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [query] =&#62; INSERT INTO component (type) VALUES( ?)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [data] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [0] =&#62; 2<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [types] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [0] =&#62; string<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [fields] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [id] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; int<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [provider_id] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; int<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; string<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [size_gender_id] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; int<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [color_id] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; int<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [quantity] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; int<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [primary] =&#62; id<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [input] =&#62; Array<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [type] =&#62; 2<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [name] =&#62; 11<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [oper] =&#62; add<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; )</p>
<p>)</p>
</div>
<p>I hope this is clear <img class="wp-smiley" src="/blog/wp-includes/images/smilies/icon_wink.gif" alt=";)" /></p>
<p>Can someone help me please?</p>
</p>
<p>Thanks in advance,</p></p>
]]></description>
        	        	<pubDate>Mon, 18 Apr 2011 13:32:57 +0300</pubDate>
        </item>
</channel>
</rss>