PDO数据未插入数据库无错误 [英] PDO data not insert into database no error

查看:109
本文介绍了PDO数据未插入数据库无错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    $query2="insert into `articles` set article_title=:article,user_name=:user,post_id=:postid      article_content=:articlecontent,article_rank=:rank,add_date=:date,article_100img=:article_100,articl    e_200img=:article_200,article_400img=:article_400,artcle_600img=:article_600,article_slug=:slug";
    $stmt=$db->prepare($query2);
    $stmt->bindParam(':article',$article_title);
    $stmt->bindParam(':articlecontent',$article_desc);
    $stmt->bindParam(':rank',$rank);
    $stmt->bindParam(':date',$date);
    $stmt->bindParam(':article_100',$x100img);
    $stmt->bindParam(':article_200',$x200img);
    $stmt->bindParam(':article_400',$x400img);
    $stmt->bindParam(':article_600',$x250img);
    $stmt->bindParam(':slug',$slug);
    $stmt->bindParam(':user',$name);
    $stmt->bindParam(':postid',$id);
    $stmt->execute();

我不知道这段代码是什么问题.所以请帮助我.

I don't know what is the problem of this code..so please help me.

推荐答案

要能够查看数据库错误,必须设置

To be able to see database errors, one have to set PDO errmode to exceptions. Exceptions are better than regular errors in many ways: they always contains a stack trace, they can be caught using try..catch or handled using dedicated error handler. And even unhandled, they act as regular PHP errors providing all the important information, following site-wide error reporting settings.

请注意,将此模式设置为连接选项也会使PDO也会因连接错误而引发异常,这非常重要.
因此,以下是正确创建PDO连接的示例:

Note that setting this mode as a connection option will let PDO throw exceptions on connection errors too, which is very important.
So, here is an example for creating a PDO connection right way:

$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    // other options 
);
$pdo = new PDO($dsn, $user, $pass, $opt);

以此方式进行连接,将始终向您通知在查询执行期间发生的所有数据库错误.请注意,您通常必须能够看到PHP错误.在实时站点上,您必须查看错误日志,因此必须进行设置

Connecting this way, you will be always notified of all database errors, occurred during query execution. Note that you have to be able to see PHP errors in general. On a live site you have to peek into error logs, so, settings have to be

error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);

在本地开发服务器上时,可以在屏幕上显示错误:

while on a local development server it's ok to make errors on screen:

error_reporting(E_ALL);
ini_set('display_errors',1);

,当然,永远不要在PDO语句之前使用错误抑制运算符(@).

and of course you should never ever use error suppression operator (@) in front of your PDO statements.

此外,由于许多错误的示例告诉您将每个PDO语句包装到try..catch块中,因此我必须做出不同的注释:

Also, due to many bad examples telling you to wrap every PDO statement into try..catch block, I have to make a distinct note:

请勿仅使用try..catch运算符来回显错误消息.未捕获的异常已经很好地实现了此目的,因为它的行为方式与其他PHP错误相同-因此,您可以使用站点范围的设置来定义行为-因此,您将收到没有此无用代码的错误消息. 尽管无条件回显错误消息可能会向潜在的攻击者透露一些敏感信息,但会造成混淆诚实的访客.

DO NOT use try..catch operator just to echo an error message. Uncaught exception is already excellent for this purpose, as it will act just the same way as other PHP errors - so, you can define the behavior using site-wide settings - so, you will have your error message without this useless code. While unconditionally echoed error message may reveal some sensitive information to a potential attacker, yet confuse a honest visitor.

  • 可以稍后添加自定义异常处理程序 ,但不是必需的.特别是对于新用户,建议使用未处理的异常,因为它们非常有用,有用和安全.
  • 仅当您要自行处理错误时才使用try..catch-例如,回滚事务.
  • A custom exception handler could be added later, but not required. Especially for new users, it is recommended to use unhandled exceptions, as they are extremely informative, helpful and secure.
  • Use try..catch only if you are going to handle the error itself - say, to rollback a transaction.

此外,我发现article_XXX字段非常无用.您不能使这些图像名称为computational吗?说,如果文章ID = 100500,则100张图片应该是100500_100.jpg?

Also, I find article_XXX fields quite useless. Can't you make these image names computational? Say, for the article id = 100500 the 100 image would be 100500_100.jpg?

这篇关于PDO数据未插入数据库无错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆