PDO INSERT not working anymore

Not in this case because it’s clear that he does not have error checking enabled. Teaching him how to generate errors is far more valuable than debugging a particular query. The actual error is fairly obvious.

I have however written and tested code for other threads.

I completely agree with teaching him to use error checking.

After your post above I decided to make the DB/table to make the test(what a pain). Anyway I made a test page using the code from post# 1 wrapped with try catch and error checking. I had no problem inserting the data into the table. Not sure what obvious error you are talking about but it worked fine for me.

@WebMachine,

Did you tried this

try{
   $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $stmt = $db->prepare($query);
   //your bindParam
   //execute .
   


}catch(PDOException $ex){

  echo $ex->getMessage();

}

By the way, I do have error checking enabled, and I am not getting any error messages. @jemz, yes I did try a “try … catch” and did not get any error message. Thanks for the suggestion.

To some of you, instead of feeling like you have to “teach” me the basics, could you please just help me figure out why my data is not being inserted.

Not in this case because it’s clear that he does not have error checking enabled. Teaching him how to generate errors is far more valuable than debugging a particular query. The actual error is fairly obvious.

I have however written and tested code for other threads.

@ahundiak, I’m not here for a lesson in php coding, thank you. If the error is obvious, please let me know. I would like to correct this and move on. Sometimes when I have stared at my code for a long time, I don’t see the ‘obvious errors’. And I’m sure that has happened to even the best of you. That is why I am asking for help.

One of the reasons I really love Sitepoint rather than other forums, is that most people on it aren’t arrogant, and genuinely want to help.

@WebMachine,

Did this really set ?

$_POST['org_name']

try to echo something inside your if statement…if it is not echoing then it is not set.

I echoed the entire array using print_r and all the data came through.

Double check that ‘:date_modified’ => $date_modified, IS or IS NOT being used in both the data building array and query. Looks like it was in earlier posts but not later i.e post#16

Error reporting would indicate this though.

In your query change “INSERT INTO organizations” to “INSERT INTO organizationsXXX” and run it. Do you get an error message?

Good test

I tried that and got this error message:

Fatal error:  Uncaught exception 'PDOException' with message  'SQLSTATE[42S02]: Base table or view not found: 1146 Table  'fakedatabasename.organizationsXXX' doesn't exist' in  C:\xampp\htdocs\Joomla25\RC_form\form_handler.php:171    Stack trace:
#0 C:\xampp\htdocs\Joomla25\RC_form\form_handler.php(171): 
PDOStatement->execute(Array)
#1 {main}
  thrown in C:\xampp\htdocs\Joomla25\RC_form\form_handler.php on line 171

It looks like it’s time to start testing each stage and seeing what happens. Let’s take a look at what happens:

var_dump(
    $db->query('SELECT COUNT(*) FROM organizations')->fetchColumn(),
    $stmt->execute($data),
    $stmt->rowCount(),
    $db->query('SELECT COUNT(*) FROM organizations')->fetchColumn()
);

It may also help to get a view on things from MySQL’s perspective. Enable statement logging, if you haven’t already. There are a few ways to do this, but I like to set a few configuration options by running the following as a SUPER user.

SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';

Then the mysql.general_log table will contain a row for each query executed. For example:

SELECT * FROM mysql.general_log ORDER BY event_time DESC LIMIT 100;

Great. That confirms error checking is enabled and that the statement was actually executed. If no error is generated when using the correct table name then it means, wait for it, the insert did work!

Whatever you are using to determine if the insert worked is invalid. Is the id for organization an auto-increment? Are you inserting in one database but checking another? Is there additional joomla code being executed? Time to be creative.

Well finally I got it to work, Thanks to all who took the time to try to help me. I couldn’t tell you exactly what was wrong, because I tried so many things but here is a list of some of the issues:

  1. I was working on my local development version, a prototype on my portfolio hosting account, and the real thing, and in the effort to multi-task (a busy time), the three versions stopped being exact copies of each other - a lesson for me.

  2. On the real thing, I had omitted to put the colon in front of the placeholder in the $data array. And I initiated a value of ‘0000-00-00’ for the variable $date_modified (which I only wanted to use in the edit feature, and added that to the INSERT query.

  3. The database on the ‘real thing’ does have ‘id’ set as a primary key, so that issue was just a slip up in my development version.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.