嗨,我是 php mysql 开发的新手。我在 Windows 上使用 wamp 服务器。 php5.3.4、mysql5.1、apache2.2。我尝试执行此代码,但我得到的只是 error.php 页面。如果表有 auto_increment id,是否有特殊的方法在 mysql 中插入?在代码中,我为 id 指定了 NULL 值。
请帮忙,请原谅我的菜鸟问题!
function addMember($firstname, $lastname, $email, $password, $bday, $gender, $maritalStatus, $education, $occupation) {
$personalQ = "INSERT INTO users_personal_profile VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if($stmt3 = $this->conn->prepare($personalQ)) {
$stmt3->bind_param('sssssssss', $firstname, $lastname, $email, $password, $bday, $gender, $education, $occupation, $maritalStatus);
$stmt3->execute();
if($stmt3->fetch()) {
$stmt3->close();
header("location: intermediate.php");
}
else {
$stmt3->close();
header("location: error.php");
}
}
}
最佳答案
压缩评论:
要查看发生了什么,您可以调整脚本:
function addMember($firstname, $lastname, $email, $password, $bday, $gender, $maritalStatus, $education, $occupation) {
$personalQ = "INSERT INTO users_personal_profile VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if($stmt3 = $this->conn->prepare($personalQ)) {
$stmt3->bind_param('sssssssss', $firstname, $lastname, $email, $password, $bday, $gender, $education, $occupation, $maritalStatus);
if( $stmt3->execute() ){
$stmt3->close();
header("location: intermediate.php");
}
else {
echo $stmt3->error;
// header("location: error.php"); // can't output headers after outputting something, but it's just for debugging
}
}
}
这应该能让您深入了解是否存在实际错误及其内容。
应在您的数据库方案中设置自动增量字段。你的表是如何定义的?第一个字段实际上标记为自动增量吗? (如果是,则 null 应该有效。) 您还可以直接在数据库中执行查询,看看效果如何。 (您可以使用控制台中的 phpmyadmin 或 navicat 或 sql。)
如果代码中需要自增值,可以通过$mysqli_stmt->insert_id;
访问
关于php - 新手 PHP MySQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6888860/