使用PDO的PHP插入 [英] PHP Insert using PDO

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

问题描述

我正在尝试使用PDO(对象)插入mysql-DB条目.在事先对PDO产生误解之后,我重新提出了这个问题.

I am trying to INSERT a mysql-DB entry with a PDO (object). After priorly missunderstanding the PDO i recreated this question.

问题是,什么都没有添加到数据库中,但是我没有抛出任何异常. 我很确定必须有一种更轻松的方法来实现我要完成的任务.但是我只是PHP和SQL的初学者.所以任何帮助或建议都将不胜感激.

The problem is, that nothing gets added to the DB but i dont get any exception thrown. I'm pretty sure there must be an easier way of achieving what i'm trying to do. But im just a beginner in PHP and SQL.. so any help or suggestions would be greatly appreciated.

我正在使用以下代码:

function INSERT($req) {
try {
    $db = new PDO('mysql:host=127.0.0.1;dbname=mcqsystem;charset=utf8', 'root', '');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    //$values = explode(",", $req);
    $values = array('question goes here', -1, 'a|b|c|d|e', '01', '0|', '0|', '0|', '0', '2', 'info text', '1', '23.01.2014', '-1', '28.12.2013 15:04:03');

    $stmt = $db->prepare("INSERT INTO _mcqs (`Question`, `PictureID`, `PossibleAnswers`, `CorrectAnswers`, `Categories`, `Courses`, `Tags`, `Variant`, `Flag`, `Information`, `Locked`, `ExamDate`, `AddedBy`, `AddedWhen`) VALUES(:question, :pictureid, :possibleanswers, :correctanswers, :categories, :courses, :tags, :variant, :flag, :information, :locked, :examdate, :addedby, :addedwhen)");
    $stmt->bindParam(':question', $values(0), PDO::PARAM_STR);
    $stmt->bindParam(':pictureid', $values(1), PDO::PARAM_INT);
    $stmt->bindParam(':possibleanswers', $values(2), PDO::PARAM_STR);
    $stmt->bindParam(':correctanswers', $values(3), PDO::PARAM_STR);
    $stmt->bindParam(':categories', $values(4), PDO::PARAM_STR);
    $stmt->bindParam(':courses', $values(5), PDO::PARAM_STR);
    $stmt->bindParam(':tags', $values(6), PDO::PARAM_STR);
    $stmt->bindParam(':variant', $values(7), PDO::PARAM_STR);
    $stmt->bindParam(':flag', $values(8), PDO::PARAM_INT);
    $stmt->bindParam(':information', $values(9), PDO::PARAM_STR);
    $stmt->bindParam(':locked', $values(10), PDO::PARAM_BOOL);
    $stmt->bindParam(':examdate', $values(11), PDO::PARAM_STR);
    $stmt->bindParam(':addedby', $values(12), PDO::PARAM_STR);
    $stmt->bindParam(':addedwhen', $values(13), PDO::PARAM_STR);

    $stmt->execute();

} catch(PDOException $ex) {
    echo "ERROR @ INSERT: " . $ex->getMessage();
    some_logging_function($ex->getMessage());
}
}

推荐答案

这是正确的方法:

function INSERT($db)
{
    $values = array('question goes here', -1, 'a|b|c|d|e', '01', '0|', '0|', '0|', '0', '2', 'info text', '1', '23.01.2014', '-1', '28.12.2013 15:04:03');
    $stmt = $db->prepare("INSERT INTO _mcqs VALUES(?,?,?,?)"); // adjust number of ?s
    $stmt->execute($values);
}

  • 不连接所有功能,而是对所有应用程序使用唯一连接
  • 请勿使用try..catch记录错误-PHP会更好地处理它
  • 不必费心列出所有字段
  • 如果您已经有数组,请不要分别绑定每个值
    • do not connect in the every function, but use sole connection for all the application
    • do not use try..catch to log an error - PHP will handle it better
    • do no bother with listing all the fields
    • do not bind each value separately if you already have an array
    • 这篇关于使用PDO的PHP插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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