开发者

how to put the value of the id of a table into another table

i have two table in a database one is register table another is type table. I'm trying to create a form that will include a drop down menu derived from the 'type' table and the values of the form will be inserted in the 'register' table. When a option is selected the id of the type will be inserted in to foreign id field of the 'register' table where the values of the form is actually posted. I'm really stuck with this problem. please help.

my form looks like this

开发者_开发技巧

enter code here

<form action="form1.php" method="post">
    Name: <input type="text" name="name" /><br/><br/>
    Username: <input type="text" name="username" /><br/><br/>
    Password: <input type="password" name="password" /><br/><br/>
    Email: <input type="text" name="email" /><br/><br/>
    You are:
    <select name="type" size="3 multiple">
        <option value="1" selected="selected">Super admin</option>
        <option value="2">Admin</option> 
        <option value="3">User</option>
    </select>
    <input type="submit" name="submit" value="Submit" /> 
    </form> 

and the tables


  • Table project.type

CREATE  TABLE IF NOT EXISTS `project`.`type` (

  `id` INT NOT NULL AUTO_INCREMENT ,

  `type` VARCHAR(45) NULL ,

  PRIMARY KEY (`id`) )

ENGINE = InnoDB;

  • Table project.register

CREATE  TABLE IF NOT EXISTS `project`.`register` (

  `id` INT NOT NULL ,

  `name` VARCHAR(45) NULL ,

  `username` VARCHAR(45) NULL ,

  `password` VARCHAR(45) NULL ,

  `email` VARCHAR(45) NULL ,

  `phone` VARCHAR(45) NULL ,


  `type_id` INT NOT NULL ,

  PRIMARY KEY (`id`) ,

  UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,

  INDEX `fk_register_type1` (`type_id` ASC) ,

  CONSTRAINT `fk_register_type1`

    FOREIGN KEY (`type_id` )

    REFERENCES `project`.`type` (`id` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB;


The value of your select options should be their id in the type table. So if the id of Super User is 10 in the project table, that should also be the value in the select box.

This way when you retrieve the value selected in the drop down box you can insert it as the foreign key in your register table.

Here's an illustration:

Project Table:

________________________
| id | type            |
________________________
| 10 | SuperSuper User |
________________________
| 11 | SuperDuper User |
________________________

Your Html:

<select name="type" size="3 multiple">
  <option value="10" selected="selected">SuperSuper User</option>
  <option value="11">SuperDuper User</option>
  ...
  ...
</select>


This is just an idea, it will not work. It should be enough to help you start though. Btw, you don't actually have to "connect" foreign ID. Yes, in school they teach you that you have to connect FOREIGN KEY (type_id) like this but you don't really need to, since DB doesn't really care if it's a foreign key or just a number.

if(isset($_POST["submit"])){
    if(empty($_POST["name"])||empty($_POST["username"])||empty($_POST["password"])||empty($_POST["email"])){
        echo "Something is wrong";
        exit;
    }
    mysql_query("INSERT INTO register (name, username, password, email, type_id) 
    VALUES ($_POST[name], $_POST[username], $_POST[password], $_POST[email], $_POST[type])");
}

Also do you want values in drop-down selected from DB or static like it is now?


EDIT: so, dropdown needs to be dynamic...

<select name="type" size="3 multiple">
<?php 
    $getType = mysql_query("SELECT id, type FROM type ORDER BY id");
    while($type = mysql_fetch_object($getType)){
        echo "<option value=\"$type->id\">$type->type</option>";
    }
?>
</select>

or something like this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜