Jump to content

Registration of a new User ! Not Inserting Into localhost Database


rajcrk

Recommended Posts

I am trying to create a registration form for users and when the user registers an email will be sent to the user and the information on the unverified user is saved in the database. The problem is that I'm getting the email but the information is not being stored in the localhost database

My PHP Code is:

<?php
// if the sign up form was submitted
if($_POST){
 
    $email = isset($_POST['email']) ? $_POST['email'] : "";
 
    // posted email must not be empty
    if(empty($email)){
        echo "<div>Email cannot be empty.</div>";
    }
 
    // must be a valid email address
    else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        echo "<div>Your email address is not valid.</div>";
    }
 
    else{
 
        include 'libs/db_connect.php';
 
        // check first if record exists
        $query = "SELECT id FROM users WHERE email = ? and verified = '1'";
        $stmt = $con->prepare( $query );
        $stmt->bindParam(1, $email);
        $stmt->execute();
        $num = $stmt->rowCount();
 
        if($num>0){
            echo "<div>Your email is already activated.</div>";
        }
 
        else{
            


            // check first if there's unverified email related
            $query = "SELECT id FROM users WHERE email = ? and verified = '0'";
            $stmt = $con->prepare( $query );
            $stmt->bindParam(1, $email);
            $stmt->execute();
            $num = $stmt->rowCount();
 
            if($num>0){
 
                // you have to create a resend verification script
                echo "<div>Your email is already in the system but not yet verified.</div>";
            }
 
            else{
                //Other Data Assignment 
                $freelancer_first_name=$_POST['freelancer_first_name']; 
                $freelancer_last_name=$_POST['freelancer_last_name'];
                $freelancer_password=$_POST['freelancer_password'];
                $freelancer_category=$_POST['freelancer_category'];
                $freelancer_city=$_POST['freelancer_city'];
                $state=$_POST['state'];
                $zip=$_POST['zip'];
                //Image Type Checking Code
                function GetImageExtension($imagetype)
                {
                if(empty($imagetype)) return false;
                switch($imagetype)
                {
                    case 'image/bmp': return '.bmp';
                    case 'image/gif': return '.gif';
                    case 'image/jpeg': return '.jpg';
                    case 'image/png': return '.png';
                    default: return false;
                    }
                }
                //Image Type Chgecking ending
                //Image Variable Assignment 
                
                if (!empty($_FILES["document_image"]["name"])) {
    $file_name=$_FILES["document_image"]["name"];
    $temp_name=$_FILES["document_image"]["tmp_name"];
    $imgtype=$_FILES["document_image"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "images/".$imagename;
     
if(move_uploaded_file($temp_name, $target_path)) {
 
    $query_upload="INSERT into 'users' ('document_image','nickname') VALUES
 
('".$target_path."','".date("Y-m-d")."')";
    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); 
     
}else{
 
   exit("Error While uploading image on the server");
}
 
}
 


                //Image Variable Assignment Ending
                //Other Data Assignment Ending
                // now, compose the content of the verification email, it will be sent to the email provided during sign up
                // generate verification code, acts as the "key"
                $verificationCode = md5(uniqid("yourrandomstringyouwanttoaddhere", true));
 
                // send the email verification
                $verificationLink = "localhost/toDil/activate.php?code=" . $verificationCode;
 
                $htmlStr = "";
                $htmlStr .= "Hi " . $email . ",<br /><br />";
 
                $htmlStr .= "Please click the button below to verify your subscription and have access to the download center.<br /><br /><br />";
                $htmlStr .= "<a href='{$verificationLink}' target='_blank' style='padding:1em; font-weight:bold; background-color:blue; color:#fff;'>VERIFY EMAIL</a><br /><br /><br />";
 
                $htmlStr .= "Kind regards,<br />";
                $htmlStr .= "<a href='https://codeofaninja.com/' target='_blank'>The Code of a Ninja</a><br />";
 
 
                $name = "The Code of a Ninja";
                $email_sender = "no-reply@codeofaninja.com";
                $subject = "Verification Link | toDil | Subscription";
                $recipient_email = $email;
 
                $headers  = "MIME-Version: 1.0\r\n";
                $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                $headers .= "From: {$name} <{$email_sender}> \n";
 
                $body = $htmlStr;
 
                // send email using the mail function, you can also use php mailer library if you want
                if( mail($recipient_email, $subject, $body, $headers) ){
 
                    // tell the user a verification email were sent
                    echo "<div id='successMessage'>A verification email were sent to <b>" . $email . "</b>, please open your email inbox and click the given link so you can login.</div>";
 
 
                    // save the email in the database
                    $created = date('Y-m-d H:i:s');
 
                    //write query
                    $query = "INSERT INTO
                                users
                            SET

                                freelancer_first_name = ?,
                                freelancer_last_name = ?,
                                freelancer_password = ?,
                                freelancer_category = ?,
                                freelancer_city = ?,
                                state = ?,
                                zip = ?,
                                document_image = ?,
                                email = ?,
                                verification_code = ?,
                                created = ?,
                                verified = '0'";
 
                    $stmt = $con->prepare($query);
                    
                    $stmt->bindParam(1, $freelancer_first_name);
                    $stmt->bindParam(2, $freelancer_last_name);
                    $stmt->bindParam(3, $freelancer_password);
                    $stmt->bindParam(4, $freelancer_category);
                    $stmt->bindParam(5, $freelancer_city);
                    $stmt->bindParam(6, $state);
                    $stmt->bindParam(7, $zip);
                    $stmt->bindParam(8, $target_path);
                    $stmt->bindParam(9, $email);
                    $stmt->bindParam(10, $verificationCode);
                    $stmt->bindParam(11, $created);
 
                    // Execute the query
                    echo $query;
                    if($stmt->execute()){
                        // echo "<div>Unverified email was saved to the database.</div>";
                    }else{
                        echo "<div>Unable to save your email to the database.";
                        //print_r($stmt->errorInfo());
                    }
 
                }else{
                    die("Sending failed.");
                }
            }
 
 
        }
 
    }
 
}
 
// show your sign up or registration form
echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='post'>";
    echo "<input type='email' name='email' placeholder='Enter your email address to subscribe' required />";
    echo "<input type='submit' value='Subscribe' />";
echo "</form>";
?>

And to redirect to this page I am using an HTML form page.

Thanks in Advance :-)

 

Link to comment
Share on other sites

I am trying to create a registration form for users and when the user registers an email will be sent to the user and the information on the unverified user is saved in the database.

 

You mean you've copied and pasted random code snippets you found somewhere on the Internet, mixed them without understanding what they do, and now you wonder why this Frankenstein code doesn't magically become alive.

 

Because programming doesn't work like this. Programming requires an actual understanding of the language and the concrete problem. You clearly don't have that, and most of us are not interested in this lazy Plz-fix-my-copypasta game.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.