PDO prepared statements stop redirecting when the database connection is in a separate file (config.php). The query works correctly (i.e. inserts properly, etc.), and the redirect works just fine when the database connection is in the document instead of in a separate file but it stops working when the db connection is moved to another file. It's not throwing any error messages.

In order to exclude the other issues that were mentioned in similar questions (error messages and the URL path). I've also removed the error messages (so that it's definitely not printing anything to the document) and tried replacing the relative path with the full URL. When the error reporting is on, it's wrapped in a try/catch block as shown.

UPDATE - The code below works, but if I replace $conn = new PDO... with require_once('config.php'); it no longer redirects (regardless of whether or not error messages are being set). I also removed any extra spaces which could be causing a problem, as well as the closing ?>.

I've also tried having the redirect both before and after the catch statement (as well as removing the try/catch block)

<?php
session_start();  

$conn = new PDO('mysql:host=localhost;dbname=click2fit', 'db_username', 'DB_Password');

try {
      $stmt = $conn->prepare('INSERT INTO customer_info (user_id, fname, lname) 
        VALUES(:user_id, :fname, :lname)');

        $stmt->bindParam(':user_id', $user_id);   
        $stmt->bindParam(':fname', $_POST['fname'], PDO::PARAM_STR);   
        $stmt->bindParam(':lname', $_POST['lname'], PDO::PARAM_STR);

       $stmt->execute(); 

      header("location: page2.html");         
      exit();
 } catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}

config.php:

<?php 
  $conn = new PDO('mysql:host=localhost;dbname=click2fit', 'db_username', 'DB_Password');
?>

config.php with error reporting turned on:

<?php

  $salt = "XXXXXXXXXXXXXXXXXXX";

  try {
   $conn = new PDO('mysql:host=localhost;dbname=click2fit', 'db_username', 'DB_Password');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
有帮助吗?

解决方案

I finally figured out that this problem was being caused by a known problem with UTF-8 encoded pages sometimes creating an extra line of code at the top of pages/included files (link to w3.org's explanation of this issue), and since these aren't visible removing any visible spaces doesn't correcting it. I hadn't even known that this was being set anywhere since all the web development environments I've used (Visual Web Developer and several others) automatically save documents in UTF-8.

The best solution that I've found is to save the files in a text editor as UTF-8 without BOM. The way to do this in Notepad++ or Visual Studio/Visual Web Developer (the two text editors that I use) are:

  • Notepad++ - Click on the 'Convert to UTF-8 without BOM' option under the Encoding Tab, and then resave the documents.
  • Visual Studio/Visual Web Developer Express 2010 - Use the Advanced Save Options to save with the correct UTF-8 encoding - Unicode (UTF-8 without signature) - Codepage 65001. In order to access the Advanced Save Options in Visual Web Developer Express, select 'Save As', and then click on the little down arrow on the 'Save' button to 'Save with Encoding'.

I also had to change the charset within the HTML document to avoid getting strange characters when I echo'd from php to an html document.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

It seems to work with utf-8 set in the html and No BOM in the PHP, but if you'd like to use No BOM in the HTML as well, I found 3 syntax options that worked in Firefox: charset=utf-8 without BOM; charset=UTF-8 no-BOM; charset=UTF-8 encoding without BOM (I'll update this once I've had a chance to test the code a bit more).

其他提示

There's a high likelihood that your sending output to the browser before your header() function, which would cause the redirect to fail.

You should wrap your query in a try/catch block to ensure that you are properly handling and logging any PDO exceptions that may be thrown.

As for debugging the problem, use your log file(s) and/or die("here") and walk through the code is the best I can offer without more details. You might need to look at your apache/IIS log file to find any exceptions.

Lastly, get rid of your closing ?> to ensure that whitespace isn't output to the browser (you mentioned you cleaned this up already, but this is the best practice that you should follow)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top