radio button reference a field in DB |
radio button reference a field in DB |
vickp07 |
Nov 5 2011, 06:40 PM
Post
#1
|
Group: Members Posts: 1 Joined: 5-November 11 Member No.: 15,786 |
I wasn't sure what to put in the TITLE sorry guys...
Okay so here is what i am trying to do....i am trying to make a page where a user can ADD users into the Database being used. It is a very simple page...the user must enter a username, firstname, lastname, password, and using RADIO buttons must distinguish if the user being added is a DOCTOR or a Receptionist My table for users in the database looks something like this: Table Users: user_id INT (PRIMARY KEY) username fname lnam pword role (INT) Now here is my QUESTION.... I want to be able to distinguish between a Doctor and a receptionist by using the Radio Buttons like i mentioned ABOVE. My IDEA was something like this. If the user being added is a Doctor and the radio button "doctor" is selected then pass the number '1' to the role field in the DB. OR if the user being added is a receptionist then pass a '2' to the role field in the DB. HOW WOULD I GO ABOUT DOING THIS TEST and passing a '1' or '2' value into the DB? Here is my code that i have so far for the 'add user' page: (THE RADIO BUTTONS USED TO DISTINGUISH ARE LOCATED AT THE VERY BOTTOM) CODE <?php $db = mysql_connect( "localhost","root", "temp1234"); mysql_select_db( "DoctorsOfficeDB"); include( "office-user.php" ); // POST handler $added = false; if( $_POST ) { // instantiate data class $postdata = new User( $_POST ); if( $postdata->validate() ) { $postdata->insert(); $added = $postdata->user; $postdata = NULL; } } ?> <html> <head> <title>Add User</title><hr /> <script src="add-user.js"></script> </head> <body> <h1>Add User</h1><hr /> <? if( $added ) { ?> <h3>User <?=$added?> successfully added</h3> <? } ?> <form action="add-user-office.php" method=POST> <table> <tr><td>username:</td> <td><input type="textbox" name="username" id="username" value="<?=$postdata->user?>"> <div id="usernameerr" style="color:red; <? if ($postdata->user_err){ echo "\">"; echo $postdata->user_err; } else { echo "display:none;\">"; } ?> </div> </td></tr> <tr><td>password:</td> <td><input type="password" name="password" id="pw1"></td></tr> <tr><td>re-type password:</td> <td><input type="password" name="password2" id="pw2"> <? if( $postdata->pass_err ) { ?> <div style="color:red;" id="pw2err"><?=$postdata->pass_err?> <? } ?> </td></tr> <tr><td colspan="2"><hr><h4>User Info:</h4></td></tr> <tr><td>First Name:</td> <td><input type="textbox" name="first_name" id="fname" value="<?=$postdata->fname?>"></td></tr> <tr><td>Last Name:</td> <td><input type="textbox" name="last_name" id="lname" value="<?=$postdata->lname?>"> <? if( $postdata->name_err ) { ?> <div style="color:red;" id="lnameerr"><?=$postdata->name_err?> <? } ?> </td></tr> <tr><td>Role:</td> <td><input type="radio" name="role" id="r1" value="<?=$postdata->role?>"> Doctor <br /></td> <td><input type="radio" name="role2" id="r2" value="<?=$postdata->role2?>"> Nurse/receptionist</td></tr> <tr><td> </td> <td><input type="submit" value="Submit"></td></tr> </form> </body> </html> Here is my other code that is a separate php file that calls functions to validate teh data being entered and does the ACTUAL SQL INSERT command. I am not sure if I need to change the INSERT statement to distinguish which role is being entered (Doctor or Receptionist) MY INSERT statement is inside the FUNCTION called insert CODE <? // a class class User { public $user, $pass, $fname, $lname, $role, $role2; public $user_err, $pass_err, $name_err; public function __construct( $post_array ) { $this->user = $_POST['username']; $this->pass = $_POST['password']; $this->pass2 = $_POST['password2']; $this->fname = $_POST['first_name']; $this->lname = $_POST['last_name']; $this->role = $_POST['role']; $this->role2 = $_POST['role2']; $this->user_err = NULL; $this->pass_err = NULL; $this->name_err = NULL; } public function validate() { // username isn't a duplicate if( !$this->user ) { $this->user_err = "Please specify username"; } else if( duplicate( $this->user ) ) { $this->user_err = "That username is already in use"; } // passwords match and are at least 6 chars if( !$this->pass || strlen( $this->pass ) < 6 ) { $this->pass_err = "Password must be at least 6 characters"; } else if( $this->pass != $this->pass2 ) { $this->pass_err = "Passwords do not match"; } // first/last name aren't blank if( !$this->fname || !$this->lname ) { $this->name_err = "Please provide a first and last name"; } return !$this->has_errors(); } public function has_errors() { return $this->user_err || $this->pass_err || $this->name_err; } public function insert() { $sql = " INSERT INTO users (username, f_name, l_name, role, pword) VALUES ( '$this->user', '$this->fname', '$this->lname', '$this->role', aes_encrypt( 'The Secret Phrase', '$this->pass' );"; mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() ); } } function duplicate( $username ) { $sql = "SELECT id FROM users WHERE username = '$username'"; $result = mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() ); return mysql_num_rows( $result ) > 0; } ?> |
Lo-Fi Version | Time is now: 26th April 2024 - 02:33 PM |