0

I have writen this pice of code that should insert into my Database some event data, but it does not insert a thing in the DB, can you tell me why?

try {
    $pdo = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
catch( PDOException $excepiton ) {
    echo "Connection error :" . $excepiton->getMessage();
}




try{
    $sql = "INSERT INTO events_DB (event_id, event_end_time, event_location, event_name) VALUES (:event_id, :event_end_time, :event_location, :event_name) ON DUPLICATE KEY UPDATE event_id = :event_id, event_end_time = :event_end_time, event_location = :event_location, event_name = :event_name";

    $stm = $db->prepare($sql);
    $stm->execute(array(":event_id" => $event[id], ":event_end_time" => $event[end_time], ":event_location" => $event[location], ":event_name" => $event[name]));

}
catch ( PDOException $exception )
{
//  decomentati sa vedeti erorile
  echo "PDO error :" . $exception->getMessage();
}

Thanks

4
  • $db is supposed to be an object with the method did you created obj ? Commented Mar 16, 2014 at 1:54
  • you are right, it was $pdo. Thanks. Now the erorr is gone, but still it doesnt insert nothing in the DB. And no other error Commented Mar 16, 2014 at 1:59
  • now you can echo your query before passing it to execute function and run that same query directly on phpmyadmin and check what error are you getting Commented Mar 16, 2014 at 2:01
  • Oke, I have some "field cannot be null". I will handle those, thanks! Commented Mar 16, 2014 at 2:06

2 Answers 2

1

The code you've posted is different than the code you're running as the posted code would result in a syntax error at parse time and never actually run.

However, what's happening is the SQL being sent to the prepare method is not valid in some way so that the result returned and stored in $stm is a boolean (false) rather than a valid statement object. Double check your SQL (you could try running it in another application such as phpMyAdmin or via the mysql command-line program) to ensure its validity. You could also add some error handling to find the cause with:

$stm = $db->prepare($sql);
if (!$stm) {
 die($db->errorInfo());
}

Edit: You've modified the posted source code which now shows use of exception handling. However, you've commented out the line that echos the exception message. This information will be useful in telling you what's causing the error condition. Uncomment to see the message (which will most likely inform you that the SQL is invalid and which part of it caused the error).

Sign up to request clarification or add additional context in comments.

2 Comments

I don't have any idea what you are saying here. Anyway, I figured it out. Now I have to edit my phpmyadmin fields here and there and it should work, I am getting a "cannot be null" error. thanks for your time
Yes thats right, its different, I have 20 fields, couldnt add all of them in the question, probably thats why I have parse errors, probably I have dont something when I was formatting.
0

Try to remove the <br> tag from the first line and a " is messing

$sql = "INSERT INTO events_DB (event_id, event_end_time, event_location, event_name);"

1 Comment

Sorry that was just bad formating. Now its formated properly. Please check it out

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.