使用 php 和 Microsoft sql 更新数据库中的行


Update row in database with php and Microsoft sql

嗨,我

目前正在创建一个博客,只是为了了解有关php,数据库等的更多信息。 我已经到了我希望能够编辑我的帖子的程度。 但是我很难让它工作...

这是我的代码到目前为止的样子:

 <?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");
?>

现在我收到错误消息:

致命错误:未捕获的异常"PDOException",并显示消息 'SQLSTATE[07002]: [Microsoft][SQL Server Native Client 11.0]COUNT 字段中不正确或语法错误' C:''inetpub''wwwroot''dev'y''post_edit.php:17 堆栈跟踪:#0 C:''inetpub''wwwroot''dev'y''post_edit.php(17): PDOStatement->execute(Array) #1 {main} throw in C:''inetpub''wwwroot''dev'y''post_edit.php 在第 17 行

第 17 行是执行数组

我也尝试过:

 <?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");
?>

这也给出了同样的错误,无法真正找到执行行需要如何更改。

编辑:

使用下面的代码时,我只会再次得到一个空白页。 :/

 <?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");
?>

将查询从:

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

自:

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

然后,您还必须将:id添加到执行数组中。

 <?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");
?>

这为我解决了它! 感谢您的帮助!