0
votes

So first of I apologize for my rocky explanation. My PHP skills need serious refinement.

The error message is saying that "memberID" is unknown. I know this isn't true because when I create a variable with memberID called "localmemberID" and echo the former, I get the memberID number.

Here's the complete error message:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'memberID' in 'field list'' in C:\xampp\htdocs\loginregister-master\addnew.php:70 Stack trace: #0 C:\xampp\htdocs\loginregister-master\addnew.php(70): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\loginregister-master\addnew.php on line 70

Here's the code for the file in question:

<?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> &nbsp; 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> &nbsp; save
        </button>
        </td>
    </tr>



    </table>

</form>






</div>






<!-- Latest compiled and minified JavaScript -->
<script src="bootstrap/js/bootstrap.min.js"></script>


</body>
</html>

Table structure

For tbl_users:

CREATE TABLE IF 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, PRIMARY KEY (userID) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;

For 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 'No', PRIMARY KEY (memberID) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

2
Are you sure there is memberID column in tbl_users table?Bhaskar Jain
you might as well need to show us your schemaMasivuye Cokile
@Bhaskar Yeah it's there.Josef Samen
@MasivuyeCokile schema?Josef Samen
your table(s) @JosefSamenMasivuye Cokile

2 Answers

0
votes

When you compare schema and your SQL - I see that you have INSERT INTO tbl_users, but columnmemberID is in table members only.

Please, update your schema of tbl_users with missing column memberID.

0
votes
INSERT INTO tbl_users(userName,userProfession,userPic,memberID) VALUES(:uname, :ujob, :upic, :umemberID )');

You are inserting your value in the memberID column, but INTO tbl_users, and memberID seems to belongs to the members table.