首先,我为我的解释道歉。我的PHP技能需要认真改进。
错误消息是" memberID
"不明。我知道这不是真的,因为当我用memberID
创建一个名为" localmemberID
"的变量时并且回应前者,我得到memberID
号码。
这里有完整的错误消息:
致命错误:未捕获的异常' PDOException'与消息 ' SQLSTATE [42S22]:未找到列:1054未知列' memberID'在 '字段列表''在C:\ xampp \ htdocs \ loginregister-master \ addnew.php:70 堆栈跟踪:#0 C:\ xampp \ htdocs \ loginregister-master \ addnew.php(70): 抛出PDOStatement-> execute()#1 {main} 第70行的C:\ xampp \ htdocs \ loginregister-master \ addnew.php
这里是相关文件的代码:
<?php
session_start();
$localmemberID = $_SESSION['memberID'];
echo $localmemberID;
error_reporting( ~E_NOTICE ); // avoid notice
require_once 'includes/config.php';
if(isset($_POST['btnsave']))
{
$username = $_POST['user_name'];// user name
$userjob = $_POST['user_job'];// user email
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$userpic = rand(1000,1000000).".".$imgExt;
// allow valid image file formats
if(in_array($imgExt, $valid_extensions)){
// Check file size '5MB'
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
// if no error occured, continue ....
if(!isset($errMSG))
{
$stmt = $db->prepare('INSERT INTO tbl_users(userName,userProfession,userPic,memberID) VALUES(:uname, :ujob, :upic, :umemberID )');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
$stmt->bindParam(':umemberID',$localmemberID);
if($stmt->execute())
{
$successMSG = "new record succesfully inserted ...";
header("refresh:5;home.php"); // redirects image view page after 5 seconds.
}
else
{
$errMSG = "error while inserting....";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
</head>
<body>
<div class="container">
<div class="page-header">
<h1 class="h2">add new user. <a class="btn btn-default" href="home.php"> <span class="glyphicon glyphicon-eye-open"></span> view all </a></h1>
</div>
<?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
</div>
<?php
}
?>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">Username.</label></td>
<td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profession(Job).</label></td>
<td><input class="form-control" type="text" name="user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
<span class="glyphicon glyphicon-save"></span> save
</button>
</td>
</tr>
</table>
</form>
</div>
<!-- Latest compiled and minified JavaScript -->
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
表格结构
对于tbl_users:
CREATE TABLE IF NOT NOT EXISTS tbl_users
(
userID
int(11)NOT NULL AUTO_INCREMENT,
userName
varchar(20)NOT NULL,
userProfession
varchar(50)NOT NULL,
userPic
varchar(200)NOT NULL,
主要关键(userID
)
)ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 51;
对于memeber:
CREATE TABLE members
(
memberID
int(11)NOT NULL AUTO_INCREMENT,
username
varchar(255)NOT NULL,
password
varchar(255)NOT NULL,
email
varchar(255)NOT NULL,
active
varchar(255)NOT NULL,
resetToken
varchar(255)DEFAULT NULL,
resetComplete
varchar(3)DEFAULT&#39; No&#39;,
主要关键(memberID
)
)ENGINE = MyISAM DEFAULT CHARSET = latin1;
答案 0 :(得分:0)
比较架构和SQL时 - 我发现您有INSERT INTO tbl_users
,但列memberID
仅在表members
中。
请更新tbl_users
的架构,但缺少列memberID
。
答案 1 :(得分:0)
INSERT INTO tbl_users(userName,userProfession,userPic,memberID) VALUES(:uname, :ujob, :upic, :umemberID )');
您在memberID
列中插入了值,但是INTO tbl_users
,并且memberID似乎属于members
表。