0

I am new to using PDO and I am getting an http 500 server error when the form is submitted.

The php page with the processing code is in the correct folder so I don't know why its throwing up a 500 error.

There is NO url rewrite going on neither .

Here is my code:

        try {
        $dbh = new PDO("mysql:host=$hostname;dbname=crm",$username,$password);

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

        $stmt = $db->prepare("INSERT INTO testdrives (forename, surname,phone,email,add1,add2,add3,city,county,postcode,car,date) VALUES (:forename, :surname,:phone,:email,:add1,:add2,:add3,:city,:county,:postcode,:car,:date)");
                $stmt->bindParam(':forename', $_POST['forename']);
                $stmt->bindParam(':surname', $_POST['surname']);
                $stmt->bindParam(':phone', $_POST['phone']);
                $stmt->bindParam(':email', $_POST['email']);
                $stmt->bindParam(':add1', $_POST['add1']);
                $stmt->bindParam(':add2', $_POST['add2']);
                $stmt->bindParam(':add3', $_POST['add3']);
                $stmt->bindParam(':city', $_POST['city']);
                $stmt->bindParam(':county', $_POST['county']);
                $stmt->bindParam(':postcode', $_POST['postcode']);
                $stmt->bindParam(':car', $_POST['car']);
                $stmt->bindParam(':date', $_POST['date']);
                $stmt->execute();
        if ($dbh->query($sql)) {
        echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
        }
        else{
        echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
        }

        $dbh = null;
        }
        catch(PDOException $e)
        {
        echo $e->getMessage();
        }
10
  • if ($dbh->query($sql)) that shouldn't even be part of your code. Apr 24, 2017 at 15:59
  • If there's a 500 error from PHP, there's a log with an actual error - if you just let it, PHP will tell you what's wrong.
    – Qirel
    Apr 24, 2017 at 15:59
  • What's the error in your PHP logs? When you enable error reporting, what do you see? 500 means "an error happened", you need to see what that error actually is.
    – David
    Apr 24, 2017 at 15:59
  • u are also using a reserved word Apr 24, 2017 at 16:00
  • 2
    @MasivuyeCokile "lose this line if ($dbh->query($sql)) {} doesn't make any sense" - Which is what I said from the very beginning........ Apr 24, 2017 at 16:05

2 Answers 2

3

Edit:

I missed the incorrect variable used for the connection being $dbh and $db in the prepare; my bad.


Original answer.

This line:

if ($dbh->query($sql)) {...}

is failing for two reasons:

  • Calling query() on what is already being prepared/executed.
  • Using a non-existant variable, $sql.

Get rid of that statement with the related brace and replace it with simply and replacing the $stmt->execute(); with:

if($stmt->execute()){

        // success

    } else{

        // error

    }

and using PDO's error handling (as you are doing now) and PHP's error reporting:

Check your logs also.

5
  • Looks like the Op is allergic in marking answers Apr 24, 2017 at 16:43
  • @MasivuyeCokile Yeah; well not much that can be done about that. "C'est la vie" ;-) Apr 24, 2017 at 16:46
  • still throwing the same error Apr 26, 2017 at 8:10
  • This error is being show in log file Call to a member function prepare() on a non-object in /myfile.php on line 13, Apr 26, 2017 at 8:16
  • @ChrisYates Ah yeah sorry about that. I missed the wrong variable; I made an edit to my answer. Apr 26, 2017 at 13:52
0

Found the problem

The above code shows

$stmt = $db->prepare

Needed to be changed to

$stmt = $dbh->prepare

Thanks for help with the other issue

Quick question how to i insert into a table that has an auto increment column ?

Not the answer you're looking for? Browse other questions tagged or ask your own question.