PHP Insert using PDO

2019-08-27 23:07发布

问题:

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

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.

I am using this code:

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());
}
}

回答1:

Here is the right way:

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);
}
  • 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


回答2:

You don't access array items by using (), You have to use [].

$stmt->bindParam(':question', $values(0), PDO::PARAM_STR);   // $values(0)  is wrong

Correct way is

$stmt->bindParam(':question', $values[0], PDO::PARAM_STR);
                                     ^ ^

This applies to all your statements, not just one.