使用php和Microsoft sql更新数据库中的行
嗨,我现在正在创建一个博客,只是为了学习更多关于php、数据库等的知识。我已经做了很多,我希望能够编辑我的文章。然而,我努力让它工作 到目前为止,我的代码是这样的:使用php和Microsoft sql更新数据库中的行,php,sql-server,database,sql-update,Php,Sql Server,Database,Sql Update,嗨,我现在正在创建一个博客,只是为了学习更多关于php、数据库等的知识。我已经做了很多,我希望能够编辑我的文章。然而,我努力让它工作 到目前为止,我的代码是这样的: <?php error_reporting(E_ALL); ini_set('display_errors', 1); $head = $_POST['title']; $short = $_POST['short']; $bread = $_POST['edit']; $author = $_POST['autho
<?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本机客户端11.0]计数
字段不正确或中的“语法错误”
C:\inetpub\wwwroot\dev\ny\post_edit.php:17堆栈跟踪:#0
C:\inetpub\wwwroot\dev\ny\post\u edit.php(17):
PDO语句->执行(数组)#1{main}抛出
第17行的C:\inetpub\wwwroot\dev\ny\post\u edit.php
第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
致:
然后还必须将: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");
?>
这帮我解决了!谢谢你的帮助 在
$\u POST['id']
哦,现在我至少收到一条错误消息…@LPK更新了POST看看:-我想你把SQL更新和Insert弄混了statements@LPK谢谢,我猜应该是这样的=$sql='updatedbo.blog\u posts SET blog\u title=$head blog\u short=$short,blog\u post=$bread,blog\u author=$author,blog_category=$cat,其中blogID=$id';但是我该怎么做脚本的执行部分呢?我试过了,但是我得到了一个空白页,我编辑了我的帖子来展示我是如何使用代码的。。。谢谢你花时间来帮助我!backticks是mysql中的分隔符。他使用的是sql server(新的PDO('sqlsrv:server..)啊,好的,我修复了它
<?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");
?>