0

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
  • 1
    You are dereferencing the items in the array wrong. You should use square brackets: $values[0]. Commented Aug 19, 2013 at 6:41

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

We can use () for array from PHP version 5.5. :)
That's great, thanks for that information. Answer stands deleted then :)
@YogeshSuthar I'm sorry but I accepted your comment and deleted my answer thinking what you noted was valid, I was asked if I did some research on it and when I did I found out that your comment is not valid and my original answer was not wrong. You can not access array items using () even in PHP 5.5, any resource you can kindly link me to?
@Your Common Sense Thank You for pointing out my mistake, helps me for future too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.