title: PDO/MySQL: Inserting with a prepared statement - 500 (Internal Server Error)
body: <p>I am writing a simple database application with PHP and MySQL. I am using ajax to run scripts server-side. I have written a database class using PDO.</p>
  
  <p>I have written various php scripts to handle inserting data, and they're all using my class's "prepare()" and "execute()" methods.</p>
  
  <p>I wrote a separate php script to create the tables and delete the tables, and also to insert sample data, and to delete all data. This is to help me return to a "known" set of data while I am building and debugging the database application.</p>
  
  <p>I have created four buttons in html, and I am calling a php script asychronously, passing a variable called "script" using GET. In my script, I have a switch statement which determines the SQL command to run. There are no variables I need to bind.</p>
  
  <p>When I click a button to execute a script, it works - if the circumstances allow. So, for example, if I click to delete all tables, it will delete the tables. However, if I click it again (and there are no tables there to delete), the javascript returns a 500 - Internal Server Error. I would like to return an error message, but can't work out where in my scripts to handle this.</p>
  
  <p>Here are my class methods:</p>
  
  <pre><code>public function prepare($query){
    // PDO prepare allows for binding of values, removes threat of SQL injection and improves query performance
    $this-&gt;statement = $this-&gt;connection-&gt;prepare($query);
  }
  
  public function bind($parameter, $value, $type = null){
    // PDO bindValue binds inputs to placeholders
    if(is_null($type)){
      switch(true){
        case is_int($value):
          $type = PDO::PARAM_INT;
          break;
        case is_bool($value):
          $type = PDO::PARAM_BOOL;
          break;
        case is_null($value):
          $type = PDO::PARAM_NULL;
          break;
        default:
          $type = PDO::PARAM_STR;
      }
    }
    $this-&gt;statement-&gt;bindValue($parameter, $value, $type);
  }
  
  public function execute(){
    // Executes the prepared statement
    return $this-&gt;statement-&gt;execute();
  }
  
  public function allresults(){
    // Returns all results
    $this-&gt;execute();
    return $this-&gt;statement-&gt;fetchAll(PDO::FETCH_ASSOC);
  }
  </code></pre>
  
  <p>And here is the key part of the php script being called with ajax:</p>
  
  <pre><code>switch ($script) {
    case "create":
        global $create_tables;
        global $database;
        $database-&gt;prepare($create_tables);
        $result = $database-&gt;execute($create_tables);
        if($result){
          echo "Success";
        }
        else{
          echo "Failed";
        }
        break;
  </code></pre>
  
  <p>One of the buttons:</p>
  
  <pre><code>&lt;button type="button" class="btn btn-success" onclick="library('newsql.php?script=create')"&gt;Create Tables&lt;/button&gt;
  </code></pre>
  
  <p>And my js:</p>
  
  <pre><code>function library(path) {
  
  
  xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 &amp;&amp; this.status == 200) {
            document.getElementById("main").innerHTML = this.responseText;
        }
    };
    xmlhttp.open("GET",path,true);
    xmlhttp.send();
  };
  </code></pre>
  
  <p>I think it has something to do with the standard return for a prepared statement, but maybe it's something to do with executing a prepared statement without binding variables, or repeating a prepared statement without changing variables?</p>