The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> radio button reference a field in DB
vickp07
post 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;
}

?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Nov 5 2011, 11:28 PM
Post #2


Jocular coder
********

Group: Members
Posts: 2,215
Joined: 31-August 06
Member No.: 43



I don't quite understand the problem, but some comments:

(1) Using 1=Doctor, 2=Receptionist is dreadfully 1960s programming. Much better to use a mnemonic character field, such as 'doc' for doctor, 'rec' for...

(2)

CODE

  <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>


What is this "$postdata"? You are showing two radio buttons, so I would expect to see something like:

CODE

<input type=radio name=role value=doc> Doctor
<input type=radio name=role value=rec> Receptionist


This invites the user to select one of the possible roles for the entry being made, and sends the appropriate value to be inserted in the DB.

Why are you sending different values for a selection of "Doctor" depending on something else? (mystery...)




--------------------
Brian Chandler
Nothing in this post constitutes "commercial solicitation". PayPal does not solicit residents of Japan. Contents may settle in transit. "Legal mind" may or may not be brain-damaged.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
geoffmerritt
post Nov 13 2011, 04:57 AM
Post #3


Member
***

Group: Members
Posts: 66
Joined: 23-December 08
From: Adelaide
Member No.: 7,394



Adding to Brian's changes you will also need to change

CODE
$this->role = $_POST['role'];
    $this->role2 = $_POST['role2'];



to

CODE
$this->role = $_POST['role'];


This will post either doc or rec to your data base.... and when querying the database you can use either doc of rec to filter the query


--------------------
Carpe Diem

I mainly code in php, so my answers will have a html and php bias. www.caboosewebdesign.com.au
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Dec 7 2011, 09:45 PM
Post #4


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



Using 'doc' or 'rec' is sooo Nineties !

Nowadays we use 'Doctor' or 'receptionist'.

After all, we got PLENTY of memory!


--------------------
Ephraim F. Moya
"Los pintos y los pendejos
Se conocen desde lejos."
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Dec 7 2011, 10:28 PM
Post #5


Jocular coder
********

Group: Members
Posts: 2,215
Joined: 31-August 06
Member No.: 43



QUOTE(Ephraim F. Moya @ Dec 8 2011, 11:45 AM) *

Using 'doc' or 'rec' is sooo Nineties !

Nowadays we use 'Doctor' or 'receptionist'.

After all, we got PLENTY of memory!


Memory isn't the problem. If you have mnemonic codes for a limited set of categories you can keep to those categories. If you allow people to just "write it all out" inevitably you end up with 'Doctor', 'Doc', 'Dr', 'Dr.', 'doctor', and whatever. It seems to me rather like the latest fashion for writing a precis of a webpage in the URL (with another bunch of attendant problems when the URL ends in ')' for example.)


--------------------
Brian Chandler
Nothing in this post constitutes "commercial solicitation". PayPal does not solicit residents of Japan. Contents may settle in transit. "Legal mind" may or may not be brain-damaged.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Dec 8 2011, 06:17 AM
Post #6


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(Brian Chandler @ Dec 7 2011, 08:28 PM) *

QUOTE(Ephraim F. Moya @ Dec 8 2011, 11:45 AM) *

Using 'doc' or 'rec' is sooo Nineties !

Nowadays we use 'Doctor' or 'receptionist'.

After all, we got PLENTY of memory!


Memory isn't the problem. If you have mnemonic codes for a limited set of categories you can keep to those categories. If you allow people to just "write it all out" inevitably you end up with 'Doctor', 'Doc', 'Dr', 'Dr.', 'doctor', and whatever. It seems to me rather like the latest fashion for writing a precis of a webpage in the URL (with another bunch of attendant problems when the URL ends in ')' for example.)

Aren't we talking about internal to the program names? Not names available for entry.

Internally your point was '1' or '2' is weak naming. If you come back to the code a couple of months (or years) later what the heck does 1 mean? You proposed calling it 'doc' or 'rec' I think it should be called isDoctor or isReceptionist to differentiate a single bit's worth of status with a MUCH MORE meaningful name.

So a statement like:

if( $isDoctor ) does exactly what
if( bit6 == 1 ) or
if( bit6 == 0 ) does
but with a better chance of avoiding errors.

---------
Incidentally: do you use Netbeans IDE? If not, RUN to download and use it. It's free, besides.


--------------------
Ephraim F. Moya
"Los pintos y los pendejos
Se conocen desde lejos."
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Dec 8 2011, 09:12 AM
Post #7


Jocular coder
********

Group: Members
Posts: 2,215
Joined: 31-August 06
Member No.: 43



This suggest there is a bitmap, and any entry can be in any number of categories... but it's a radio button, so one value we can call category, with values (represented by an enum, if you like) which are 'doctor', 'recep...' etc. So it's not if($isDoctor), it's if($categ == 'doc[...]').

Personally I like short names, because it saves typing, but probably tend to err on the side of overabbreviating.

You know that Brian Kernighan (I think, or Dennis Ritchie, obviously) was asked if he did Unix again, would he do anything differently? His reply: "Yes, I'd have called creat() create()."

What does Netbeans do, apart from presumably working wonders...?


--------------------
Brian Chandler
Nothing in this post constitutes "commercial solicitation". PayPal does not solicit residents of Japan. Contents may settle in transit. "Legal mind" may or may not be brain-damaged.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Dec 8 2011, 02:30 PM
Post #8


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(Brian Chandler @ Dec 8 2011, 07:12 AM) *

This suggest there is a bitmap, and any entry can be in any number of categories... but it's a radio button, so one value we can call category, with values (represented by an enum, if you like) which are 'doctor', 'recep...' etc. So it's not if($isDoctor), it's if($categ == 'doc[...]').

Personally I like short names, because it saves typing, but probably tend to err on the side of overabbreviating.

You know that Brian Kernighan (I think, or Dennis Ritchie, obviously) was asked if he did Unix again, would he do anything differently? His reply: "Yes, I'd have called creat() create()."

What does Netbeans do, apart from presumably working wonders...?


Brian,

There's a pretty good description of Netbeans on Wikipedia.

I use it to develop php programs on my web sites. Basically it is a black box that contains four different spigots. One for ftp, one for direct memory access, one for an external backup system and the last one for the languages that it supports. (See Wikipedia)

I use the ftp port directly into my sites, I use the dma for the local mirror and backup it maintains, My subversion backup is on a memory stick on usb and finally I use the php plug in to provide the editor, syntax checker, other checkers, etc.

Just having one program to do all these things is heaven. That it works as well as it does is a valuable plus. AND IT'S FREE.

I use it on Windows 7. They say it works on Linux. I've never tried it on Linux. I consider that it's worth buying a Win7 box just to be able to run Netbeans.

If there's a down side it's that it takes a large effort to learn all the various things that it can do. Fortunately, it can be useful while you're learning.

EFM


--------------------
Ephraim F. Moya
"Los pintos y los pendejos
Se conocen desde lejos."
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th August 2014 - 11:49 PM