Update row in database with php and Microsoft sql
<p>Hi im currently creating a blog, just to learn abit more about php, databases etc. Ive gotten so far that i want to be able to edit my posts. however i struggle to get it working...</p>

<p>this is what my code looks so far:</p>

<pre><code> &lt;?php
 error_reporting(E_ALL); ini_set('display_errors', 1);
 $head =  $_POST['title'];
 $short = $_POST['short'];
$bread = $_POST['edit'];
$author = $_POST['author'];
$cat = $_POST['cat'];
$id = $_POST['id'];

 $db = new PDO('sqlsrv:server=localhost;Database=blog', '*****', '*********');
$db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = 'UPDATE dbo.blog_posts SET (blog_title, blog_short, blog_post, blog_author, blog_category) VALUES (:head, :short, :bread, :author, :cat) WHERE blogID=$id';

$query = $db-&gt;prepare( $sql );
$query-&gt;execute( array(':head'=&gt;$head, ':short'=&gt;$short, ':bread'=&gt;$bread, ':author'=&gt;$author, ':cat'=&gt;$cat ) );
header("Location: index.php");
?&gt;
</code></pre>

<p>now i get the error msg:</p>

<blockquote>
  <p>Fatal error: Uncaught exception 'PDOException' with message
  'SQLSTATE[07002]: [Microsoft][SQL Server Native Client 11.0]COUNT
  field incorrect or syntax error' in
  C:\inetpub\wwwroot\dev\ny\post_edit.php:17 Stack trace: #0
  C:\inetpub\wwwroot\dev\ny\post_edit.php(17):
  PDOStatement->execute(Array) #1 {main} thrown in
  C:\inetpub\wwwroot\dev\ny\post_edit.php on line 17</p>
</blockquote>

<p>line 17 is the execute array</p>

<p>i have also tried:</p>

<pre><code> &lt;?php
 error_reporting(E_ALL); ini_set('display_errors', 1);
 $head =  $_POST['title'];
 $short = $_POST['short'];
$bread = $_POST['edit'];
$author = $_POST['author'];
$cat = $_POST['cat'];
$id = $_POST['id'];

 $db = new PDO('sqlsrv:server=localhost;Database=blog', '*****', '*****');
$db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = 'UPDATE dbo.blog_posts SET blog_title=(:head), blog_short=(:short), blog_post=(:bread), blog_author=(:author), blog_category=(:cat) WHERE blogID=:id';

$query = $db-&gt;prepare( $sql );
$query-&gt;execute( array(':head'=&gt;$head, ':short'=&gt;$short, ':bread'=&gt;$bread, ':author'=&gt;$author, ':cat'=&gt;$cat ) );
header("Location: index.php");
?&gt;
</code></pre>

<p>that also gives the same error, cant really find how the execute line needs to be changed.</p>

<p>Edit:</p>

<p>When using this code below i only get a blank page again.. :/</p>

<pre><code> &lt;?php
 error_reporting(E_ALL); ini_set('display_errors', 1);
 $head =  $_POST['title'];
 $short = $_POST['short'];
$bread = $_POST['edit'];
$author = $_POST['author'];
$cat = $_POST['cat'];
$id = $_POST['id'];

 $db = new PDO('sqlsrv:server=localhost;Database=blog', '******', '*****');
$db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = UPDATE `blog_posts` SET `blog_title` = :head, `blog_short` = :short, `blog_post`  = :bread, `blog_author` = :author, `blog_category` = :cat WHERE `blogID` = :id;

$query = $db-&gt;prepare( $sql );
$query-&gt;execute( array(':id'=&gt;$id, ':head'=&gt;$head, ':short'=&gt;$short, ':bread'=&gt;$bread, ':author'=&gt;$author, ':cat'=&gt;$cat ) );

header("Location: index.php");
?&gt;
</code></pre>