Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't Inserting data from Json Object work?
    primarykey
    data
    text
    <p>I don't know why insert values in my table doesn't work using foreach to traverse Json Object.. when I see the table, fields are empty.</p> <p><strong>Json Object (from jquery.ajax)</strong></p> <pre><code>[{"marca":"Cisco","producto":"UCS","subproducto":"Nexus"},"marca":"Citrix","producto":"Networking","subproducto":"Netscaler"}] </code></pre> <p><strong>print_r($data)</strong></p> <pre><code>Array ( [0] =&gt; Array ( [marca] =&gt; Cisco [producto] =&gt; UCS [subproducto] =&gt; Nexus ) [1] =&gt; Array ( [marca] =&gt; Citrix [producto] =&gt; Networking [subproducto] =&gt; Netscaler ) ) </code></pre> <p><strong>PHP CODE</strong></p> <pre><code>$data = json_decode($this-&gt;dataNewPoliza['alcances'], true); $marca; $producto; $subproducto; $sqlAlc = "INSERT INTO t_poliza_alcanceproductos VALUES (:idp,:m,:p,:s)"; $resultAlc = $this-&gt;dbConnect-&gt;prepare($sqlAlc) or die ($sqlAlc); foreach ($data as $key =&gt; $value) { foreach ($value as $mps =&gt; $valuemps) { if($mps == 'marca') { $marca = $valuemps; } if($mps == 'producto') { $producto = $valuemps; } if($mps == 'subproducto') { $subproducto = $valuemps; } } $resultAlc-&gt;bindValue(':idp',$id_poliza,PDO::PARAM_INT); $resultAlc-&gt;bindValue(':m',$marca,PDO::PARAM_INT); $resultAlc-&gt;bindValue(':p',$producto,PDO::PARAM_INT); $resultAlc-&gt;bindValue(':s',$subproducto,PDO::PARAM_INT); if(!$resultAlc-&gt;execute()) { return false; } else { return true; } } </code></pre> <p>Hope to get some help.</p> <p><strong>Solved</strong></p> <p>The problem was that I could not do return false or true within the foreach and as said @zerkms , I don't need nested loop, and this is final result:</p> <pre><code>$data = json_decode($this-&gt;dataNewPoliza['alcances'], true); $sqlAlc = "INSERT INTO t_poliza_alcanceproductos VALUES (:idp,:m,:p,:s)"; $resultAlc = $this-&gt;dbConnect-&gt;prepare($sqlAlc) or die ($sqlAlc); foreach ($data as $key =&gt; $value) { $resultAlc-&gt;bindValue(':idp',$id_poliza,PDO::PARAM_INT); $resultAlc-&gt;bindValue(':m',$value['marca'],PDO::PARAM_INT); $resultAlc-&gt;bindValue(':p',$value['producto'],PDO::PARAM_INT); $resultAlc-&gt;bindValue(':s',$value['subproducto'],PDO::PARAM_INT); $resultAlc-&gt;execute(); } return true; </code></pre> <p>Thanks for ur help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload