简体   繁体   中英

How to insert a field of data in mysql from another table, based from user input from a form?

I am developing a system in which people can be added to a database using mysql and php. The basis of my form is that an administrator can add person-types and person-roles, and these are applied to the user when their details are entered in the form. For example, if this system was applied to a school environment the person type could be a teacher, or a cook, and the person role could be a parent, for the person type does not exclude the person role. I have had various problems with this system for that the foreign keys don't update as my system uses MYISAM type tables, but if I use INNODB tables my data entry will not work. I am seeking ways in which I can use queries for the meantime to get this working as it is for a university project, and my deadline is ever looming. However I do mean to get this system working, so will seek ways to get it fully functioning a bit later in the year. I have javascript in place for error handling, and php blocks of code to help prevent against sql injection attacks.

When an administrator is entering a user, there is a drop down menu which is populated by the fields in the person type table, this is done by the Person_Type_Value_Description field in the person-type table. Each row in the person type table would have an Person_Type_ID . I need a query that can find the Person_Type_Value_Description , call the Person_Type_ID and store it in a variable so that it can be parsed through to the person type id in the person table. The basic layout of my tables can be seen below from the creation scripts.

CREATE TABLE Person_Type(
Person_Type_ID int auto_increment NOT NULL,
Person_Type_Value_Description varchar(150) NOT NULL,
Create_Date datetime NOT NULL NOT NULL,
Modify_Date datetime NOT NULL  NOT NULL,
Archive char(1) NULL,
CONSTRAINT PK_Person_Type_ID PRIMARY KEY (Person_Type_ID)
 ) ;

...

CREATE TABLE Person(
Person_ID int auto_increment NOT NULL,
Person_Type_ID int NOT NULL,
Create_Date datetime NOT NULL ,
Modify_Date datetime NOT NULL ,
First_Name varchar(50) NOT NULL,
Surname varchar(50) NOT NULL,
DOB date NOT NULL,
Gender char(1) NOT NULL CHECK (Gender ='f' OR Gender ='m'),
Archive char(1) NULL,
Allergies varchar(200) NOT NULL,
Dietry_Requirements varchar(200) NOT NULL,
Disabilities varchar(200) NOT NULL,
Medicine_Requirements varchar(200) NOT NULL,
username varchar (30) NOT NULL,
password varchar (30) NOT NULL,
CONSTRAINT PK_Person_ID PRIMARY KEY (Person_ID),
CONSTRAINT FK_Person_Type_ID FOREIGN KEY (Person_Type_ID)
REFERENCES Person_Type (Person_Type_ID)) ;

EDIT

Below is the code I have tried to get working. I need to store the result of the query so I can parse it to the Person table through an insert command. Thank you for commenting.

The problem I am having is that the Person_Type_ID in the Person table is always setting to 0, because of the NOT NULL command, however it is not updating based off my queries.

// drawing the information from the Person_Type table.
$sql9 = "(SELECT Person_Type_ID
                 FROM Person_Type pt
                WHERE pt.Person_Type_Value_Description =  $Person_Type_Value_Description)";
                $result = @mysql_query($sql9);

//inserting into the person table. **All filed work except for the Person_Type_ID
$qry = "INSERT INTO Person (Person_Type_ID, Create_Date, Modify_Date, First_Name, Surname, DOB, Gender, Allergies, Dietry_Requirements, Disabilities, Medicine_Requirements, username, password) 
   VALUES('$sql9', NOW(), NOW(), '$First_Name', '$Surname', '$DOB', '$Gender', '$Allergies', '$Dietry_Requirements', '$Disabilities', '$Medicine_Requirements', '$username', '$password')" ;
    $result = @mysql_query($qry);

How it is called to display in the form

<?

$persontype = array();

$qry = mysql_query("SELECT Person_Type_Value_Description FROM Person_Type ORDER BY Person_Type_ID");

while($res = mysql_fetch_array($qry)) {
$persontype[$res['Person_Type_Value_Description']] = $res['Person_Type_Value_Description'];
}

?> 


//creates the drop down
<?
function createDropdown($arr, $frm) {
foreach ($arr as $key => $value) {
echo '<option value="'.$value.'">'.$value.'</option>';
}
echo '</select>';
}
?>

The administrator would then select this from a simple drop down menu.

<td align="right"><div align="left">Person Type* </div></td>           
<td><select name="Person_Type_Value_Description" id="Person_Type_Value_Description">
 <option value="">Select One...</option>
<?php createDropdown($persontype, 'frmpersontype'); ?>
  </select>
      </td>
        </tr> 

I think the problem is here

// drawing the information from the Person_Type table.
$sql9 = "(SELECT Person_Type_ID
             FROM Person_Type pt
            WHERE pt.Person_Type_Value_Description =  $Person_Type_Value_Description)";
$result = @mysql_query($sql9);

//inserting into the person table. **All filed work except for the Person_Type_ID
$qry = "INSERT INTO Person 
    (Person_Type_ID, Create_Date, Modify_Date, First_Name, Surname, DOB, Gender,
     Allergies, Dietry_Requirements, Disabilities, Medicine_Requirements, 
     username,password) 
  VALUES('$sql9', NOW(), NOW(), '$First_Name', '$Surname', '$DOB', '$Gender', 
    '$Allergies', '$Dietry_Requirements', '$Disabilities', '$Medicine_Requirements', 
    '$username', '$password')" ;
$result = @mysql_query($qry);

You're inserting the actual query in $sql9 rather than the result.

You need something like this:

$result = @mysql_query($sql9);
$row = mysql_fetch_assoc( $result );
$personTypeID = intval( $row['Person_Type_ID'] );

//inserting into the person table. **All filed work except for the Person_Type_ID
$qry = "INSERT INTO Person 
    (Person_Type_ID, Create_Date, Modify_Date, First_Name, Surname, DOB, Gender,
     Allergies, Dietry_Requirements, Disabilities, Medicine_Requirements, 
     username,password) 
  VALUES($personTypeID etc.

As a side-note, it's recommended not to use the mysql_ family of functions any more. You should use mysqli_ or PDO, preferably with prepared statements to escape input automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM