Ok I have a file called dbc.php running on my WAMP server. In that file, I am running the following PDO statements:
<?php
require('dbc.php');
$q = $dbc -> prepare(
"INSERT INTO accounts
(fname, lname, email, password, username, gender)
VALUES
开发者_如何学JAVA (?, ?, ?, ?, ?, ?)"
);
$q -> execute(
array(
$_POST['fname'],
$_POST['lname'],
$_POST['email'],
$_POST['password'],
$_POST['username'],
$_POST['gender']
)
);
?>
I have double checked that the connection to the database is working fine, and I have double checked my fields in mysql. I have no idea where the problem is but it's probably something simple it is quite late here.
There's not a lot to go on here, so let me throw a bunch of stuff at you and see what sticks.
First, you say that this code isn't issuing any errors although it also isn't inserting your data into the database.
PDO has a few different ways of issuing errors. It can return error codes, throw E_WARNING
errors, or issue PDOException
exceptions. By default, PDO will set error codes silently. You need to call errorCode()
or errorInfo()
on the object you're operating on.
In your case, your code should look a bit like this simplified example:
$q = $dbc->prepare(...);
if ($q->errorCode) {
// Display error message
}
$q->execute(...);
if ($q->errorCode) {
// Display error message
}
This way, if you have an error in your SQL or with your data, PHP/PDO will display a nice error message to you.
You can also set one of the other error modes when you create the PDO object:
try {
$dbh = new PDO(DB_DSN,
DB_USER,
DB_PASS,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
If you set ERRMODE_EXCEPTION
, then you can create a try/catch block as shown above to echo out the error message when an exception is caught. Similarly, ERRMODE_WARNING
, PHP will issue error messages if you have your error_reporting()
setting set high enough.
If you're using ERRMODE_WARNING
, you need to ensure that your PHP is set to report E_WARNING
errors. I always suggest using error_reporting(E_ALL | E_STRICT);
for development, but as long as you have E_WARNING
set, you should see PDO errors in this mode.
Please ensure you are reporting errors, and update your question with the exact error message you're seeing. I find it hard to believe that that code is executing without errors while failing to insert any data.
Having said that, if you're not getting any errors and you're not inserting data, it might be a good idea to review your code to ensure that the code in your question is even being executed. It could be being bypassed by a failed if
condition or something.
Finally, you should be reviewing each of the $_POST
indexes you're using in your code, to ensure that they hold a value, and that the value is of the proper type for your SQL. Using a $_POST
index that hasn't been set should raise an error (E_NOTICE
, IIRC), but again you could have your error_reporting()
level too low to know for sure.
You could try something like this:
$postIndexes = array(
"fname", "lname", "email",
"password", "username", "gender"
);
foreach ($postIndexes as $index) {
$missingIndexes = array();
if (!isset($_POST[$index] || empty($_POST[$index])) {
$missingIndexes[] = $index;
}
if ($missingIndexes) {
echo "The following required indexes were missing:\n";
echo "<ul>\n";
foreach ($missingIndexes as $index) {
echo "<li>" . $index . "</li>\n";
}
echo "</ul>\n";
}
}
精彩评论