DannyZA Posted April 4 Share Posted April 4 Greetings, I'm trying to build a system where the superAdmin can add users . I have add_user.php <!--begin::Form--> <form role="form" method="post" action="insert/user.php" class="ajax" id="insert"> <div class="kt-portlet__body"> <div class="form-group row"> <div class="col-lg-6"> <label>Username</label> <input type="text" name="username" id="username" class="form-control input-circle-right" required /> <span class="form-text text-muted">Please enter a valid username</span> </div> <div class="col-lg-6"> <label>Full Name</label> <input type="text" name="firstname" id="firstname" class="form-control input-circle-right" required /> <span class="form-text text-muted">Please add the user's fullname</span> </div> </div> <div class="form-group row"> <div class="col-lg-6"> <label>Password</label> <input type="text" name="password" id="password" class="form-control input-circle-right" required /> <span class="form-text text-muted">Please enter a password for the user</span> </div> <div class="col-lg-6"> <label>Retype Password</label> <input type="text" name="password_again" id="password_again" class="form-control input-circle-right" required /> <span class="form-text text-muted">Please retype the user's password</span> </div> </div> <div class="form-group row"> <div class="col-lg-6"> <label>Email</label> <input type="text" name="email_address" id="email_address" class="form-control input-circle-right" required /> <span class="form-text text-muted">Please enter the user's email address</span> </div> <div class="col-lg-6"> <label>Status</label> <select class="form-control" name="status" id="status"> <option value="A">Active</option> <option value="I">Inactive</option> </select> <span class="form-text text-muted">Please select the status of the user account</span> </div> </div> </div> <div class="kt-portlet__foot"> <div class="kt-form__actions"> <div class="row"> <button type="submit" class="btn btn-primary">Submit</button> <input type="hidden" name="token" value="<?php echo Token::generate() ?>" /> </div> </div> </div> </div> </form> <!--end::Form--> </div> <!--end::Portlet--> </div> </div> <!-- END PAGE CONTENT--> </div> </div> <!-- END CONTENT --></div> <!-- end:: Content --> </div> AND add_user.php //start insert into DB if (Input::exists()) { $response = ""; if (Token::check(Input::get('token'))) { $validate = new Validate(); $validation = $validate->check($_POST, array( 'username' => array( 'required' => true, 'min' => 2, 'max' => 20, 'unique' => 'user_info' ), 'password' => array( 'required' => true, 'min' => 6 ), 'password_again' => array( 'required' => true, 'matches' => 'password' ), 'firstname' => array( 'required' => true, 'min' => 2, 'max' => 50 ) )); if ($validate->passed()) { $userz = new User(); $salt = Hash::salt(32); try{ $userz->create(array( 'username' => Input::get('username'), 'password' => Hash::make(Input::get('password'), $salt), 'salt' => $salt, 'firstname' => Input::get('firstname'), 'surname' => Input::get('surname'), 'group' => 2, 'reg_status' => 'A', 'role' => 'A', 'email_address' => Input::get('email_address') )); $response .= "Success! Thank you."; echo $response; }catch(Exception $e){ $response .= "Failed! Please try again"; echo $response; } }else{ $myArr = array(); foreach ($validate->errors() as $error) { $response .= $error."<br>"; echo $response; } } } }else{ }//end insert into DB The challenge i am facing, when i check on my database table see nothing is added, and i would also like user to get activation code via email after being added. Kindly advise on what i am a programming student and new to PHP Quote Link to comment https://forums.phpfreaks.com/topic/319666-php-add-new-user-and-email-verification/ Share on other sites More sharing options...
ginerjm Posted April 4 Share Posted April 4 What have you done to try and debug this? Too much code for us to be asked to read thru and try and decipher. BTW - do you always write your code with so many indents and blank lines to make it harder to follow? Quote Link to comment https://forums.phpfreaks.com/topic/319666-php-add-new-user-and-email-verification/#findComment-1620400 Share on other sites More sharing options...
mac_gyver Posted April 4 Share Posted April 4 do you have php's error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your development system, so that php will help you by reporting and displaying all the errors it detects? are you using php8+, where exceptions are the default setting for database statement errors, or if using < php8, have you enabled exceptions for errors for the database extension you are using? when your code/data doesn't work, you need to find at what point they are doing what you expect and at what point they are not. the problem lies between those two points. if all you have done is run your code and notice that it doesn't insert the expected data, you haven't narrowed down the problem to a single line of code. you need to use debugging techniques, such as using var_dump() on data, to both determine which exaction path your code is taking and what data values it is using, to find out exactly where your code/data stops doing what you expect. well written code, with sufficient error handling and validation logic, should either work or it should tell you (display/log) why it doesn't work. every data validation error should get handled. your if (Token::check(Input::get('token'))) validation doesn't do anything if the token check is a false value, indicating either a programming mistake or someone/something hasn't visited/requested the form to generate a token. you need to add an else conditional branch to display (during development) or log (when on a live server) relevant information when this occurs. finally, you have some problems in the posted form/form processing - your user ->create() method is using a surname field, that doesn't exist in the form your form has a status field, that isn't being used if you want the user to be required to make a specific choice for the status field, the first <option ...> needs to be a prompt to make a choice, with an empty value attribute the email field is not being validated both the username and email fields must be defined as unique indexes in the database table. you must then have exception error handling for the insert query that tests for a duplicate index error (number), determines which or both of the submitted values where duplicates, and sets up user error(s) for the duplicate values. the foreach() loop displaying the validation errors, is concatenating each error onto the existing $response. when there is more than one error, the $response will get echoed multiple times, with a growing number of errors in it. i recommend that you just implode() the array of errors using a '<br>' tag and output the result. the form is submitting to insert/user.php. that doesn't correspond to the filename of the posted code. Quote Link to comment https://forums.phpfreaks.com/topic/319666-php-add-new-user-and-email-verification/#findComment-1620402 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.