Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Error When Run In Site
    text
    copied!<p>I have a function that generates and runs an mysql update query. When I run the generated code in phpMyAdmin I have no troubles, however when I try running it from my site I get an error. What is odd is I am using the same format for several other update queries and they are working flawlessly.</p> <p>MySQL Query:</p> <pre><code>UPDATE `capc`.`bio_positions` SET `Pos_Name` = 'IT Specialist' WHERE `bio_positions`.`Pos_ID` = 63;UPDATE `capc`.`bio_positions` SET `Pos_Company` = 'CSG' WHERE `bio_positions`.`Pos_ID` = 63; </code></pre> <p>Error:</p> <pre><code>Could not get data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE `capc`.`bio_positions` SET `Pos_Company` = 'CSG' WHERE `bio_position' at line 1 </code></pre> <p>Code Used To Generate MySQL Query</p> <pre><code>function update($rank, $name, $company) { if (!empty($rank) &amp;&amp; $rank !== $this-&gt;Pos_Rank) { $sql = "UPDATE `capc`.`bio_positions` SET `Pos_Rank` = " . $rank . " WHERE `bio_positions`.`Pos_ID` = " . $this-&gt;Pos_ID . ";"; } if (!empty($name) &amp;&amp; $name !== $this-&gt;Pos_Name) { $sql .= "UPDATE `capc`.`bio_positions` SET `Pos_Name` = '" . $name . "' WHERE `bio_positions`.`Pos_ID` = " . $this-&gt;Pos_ID . ";"; } if (!empty($company) &amp;&amp; $company !== $this-&gt;Pos_Company) { $sql .= "UPDATE `capc`.`bio_positions` SET `Pos_Company` = '" . $company . "' WHERE `bio_positions`.`Pos_ID` = " . $this-&gt;Pos_ID . ";"; } echo "&lt;br&gt;" . $sql . "&lt;br&gt;"; if (!empty($sql)) { $capc = new CAPC; $capc-&gt;query($sql); Bio_Position($this-&gt;Pos_ID); } } </code></pre> <p>Updated Update Function From Answers That Works</p> <pre><code>function update($rank, $name, $company) { $capc = new CAPC; $sql = "UPDATE `capc`.`bio_positions` SET "; $go = 0; if (!empty($rank) &amp;&amp; $rank !== $this-&gt;Pos_Rank) { $sql .= " `Pos_Rank` = " . $rank; $go++; } if (!empty($name) &amp;&amp; $name !== $this-&gt;Pos_Name) { if($go &gt; 0){ $comma = ","; } $sql .= $comma . " `Pos_Name` = '" . $name . "'"; $go++; } if (!empty($company) &amp;&amp; $company !== $this-&gt;Pos_Company) { if($go &gt; 0){ $comma = ", "; } $sql .= $comma . " `Pos_Company` = '" . $company . "'"; $go++; } $sql .= " WHERE `bio_positions`.`Pos_ID` = " . $this-&gt;Pos_ID . ";"; if (!empty($sql) &amp;&amp; $go &gt; 0) { //echo $sql . "&lt;br&gt;"; $capc = new CAPC; $capc-&gt;query($sql); } } </code></pre>
 

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