简体   繁体   English

如何根据用户从表单输入的数据,在另一个表的mysql中插入数据字段?

[英]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. 我正在开发一个系统,其中可以使用mysql和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. 我的系统存在各种问题,因为系统使用MYISAM类型表时,外键不会更新,但是如果使用INNODB表,则数据输入将不起作用。 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. 我有用于错误处理的javascript和php代码块来帮助防止sql注入攻击。

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. 管理员输入用户时,将出现一个下拉菜单,该菜单由人员类型表中的字段填充,这是由人员类型表中的Person_Type_Value_Description字段完成的。 Each row in the person type table would have an Person_Type_ID . 人员类型表中的每一行都有一个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. 我需要一个查询,该查询可以找到Person_Type_Value_Description ,调用Person_Type_ID并将其存储在变量中,以便可以将其解析为人员表中的人员类型ID。 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. 我需要存储查询的结果,以便可以通过插入命令将其解析到Person表。 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. 我遇到的问题是,由于NOT NULL命令的缘故 ,Person表中的Person_Type_ID始终设置为0,但是它不是基于我的查询进行更新。

// 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. 您将在$ sql9中而不是结果中插入实际查询。

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. 作为附带说明,建议不要再使用mysql_系列函数。 You should use mysqli_ or PDO, preferably with prepared statements to escape input automatically. 您应该使用mysqli_或PDO,最好使用准备好的语句来自动转义输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL / PHP:如何将登录的用户ID插入另一个表,该表从用户输入的表单收集数据 - MySQL/PHP: How to insert logged in user id into another table that is gathering data from a form that the user is inputting 从另一个表以及MYSQL和PHP中的表单插入数据 - Insert data from another table and also from form in MYSQL and PHP 如何合并来自表单和来自三个mysql表的数据,以使用php将数据插入另一个表中? - How can I combine data from form and from three mysql tables to insert data in another table with php? 如何使用php将来自mysql的选定数据插入mysql中的另一个表? - How to insert selected data from mysql with php to another table in mysql? 如何从表单字段中提取多行数据并插入到mysql中? - How to extract multiple lines of data from a form field and insert in to mysql? 将数据从一个表插入另一个表 - MySQL - Insert Data From One Table To Another - MySQL 如何从两个源表单和另一个表插入mysql数据 - How to insert mysql data from two source form and other table 如何在mysql中将数据从一个表插入到另一个表? - How do I insert data from one table to another in mysql? 如何从另一个数据库 MYSQL 插入表 - How insert in table from another database MYSQL 如何将数据从一个表插入到另一个表 - How to insert data from a table to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM