Update row in database with php and Microsoft sql

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...

this is what my code looks so far:

 <?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->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->prepare( $sql );
$query->execute( array(':head'=>$head, ':short'=>$short, ':bread'=>$bread, ':author'=>$author, ':cat'=>$cat ) );
header("Location: index.php");
?>

now i get the error msg:

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

line 17 is the execute array

i have also tried:

 <?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->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->prepare( $sql );
$query->execute( array(':head'=>$head, ':short'=>$short, ':bread'=>$bread, ':author'=>$author, ':cat'=>$cat ) );
header("Location: index.php");
?>

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

Edit:

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

 <?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->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->prepare( $sql );
$query->execute( array(':id'=>$id, ':head'=>$head, ':short'=>$short, ':bread'=>$bread, ':author'=>$author, ':cat'=>$cat ) );

header("Location: index.php");
?>

Upvotes: 1

Views: 3592

Answers (2)

user5653854
user5653854

Reputation:

Change your query from:

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

To:

UPDATE dbo.blog_posts
SET blog_title = :head,
    blog_short = :short, 
    blog_post  = :bread,
    blog_author = :author,
    blog_category` = :blog_category
WHERE blogID = :id

Then you also have to add :id to your execute array.

Upvotes: 1

 <?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'];
$postdate = date('Y-m-d H:i:s');

 $db = new PDO('sqlsrv:server=localhost;Database=blog', '*****', '*****');
$db->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, blog_date= :postdate WHERE blogID= :id';


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

header("Location: index.php");
?>

This solved it for me! thanks for helping!

Upvotes: 0

Related Questions