我正在为一个类开发一个桌面RPG Monster数据库,我需要用以下参数将值插入Monster表:Monster Table。
CREATE TABLE Monsters (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
HP int unsigned NOT NULL,
MP int unsigned NOT NULL,
AC int unsigned NOT NULL,
MonsterType_ID int NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(MonsterType_ID) REFERENCES MonsterType(ID) ON DELETE CASCADE
) ENGINE=InnoDB;
我还有一个MonsterType Table
CREATE TABLE MonsterType (
ID int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB;
我已经设置了PHP代码:PHP Monster Query
if (isset($_POST["submit"])) {
if( (isset($_POST["Name"]) && $_POST["Name"] !== "") &&
(isset($_POST["HP"]) && $_POST["HP"] !== "") &&
(isset($_POST["MP"]) && $_POST["MP"] !== "") &&
(isset($_POST["AC"]) && $_POST["AC"] !== "") &&
(isset($_POST["MonsterType_ID"]) && $_POST["MonsterType_ID"] !== "") ) {
$query = "INSERT INTO Monsters ";
$query .= "(Name, HP, MP, AC, MonsterType_ID) ";
$query .= "VALUES (";
$query .= "'".$_POST["Name"]."',";
$query .= "'".$_POST["HP"]."',";
$query .= "'".$_POST["MP"]."',";
$query .= "'".$_POST["AC"]."',";
$query .= "'".$_POST["MonsterType_ID"]."');";
$result = $mysqli->query($query);
当我转到Add Monsters时,Monster Type字段只接受与MonsterType.ID索引对应的整数值。相反,我希望能够在文本字段中键入MonsterType Names
INSERT INTO MonsterType (Name)
VALUES
('Abberation'),
('Beast'),
('Celestial'),
('Construct'),
('Dragon'),
('Elemental'),
('Fey'),
('Fiend'),
('Giant'),
('Humanoid'),
('Monstrosity'),
('Ooze'),
('Plant'),
('Undead');
并将相应类型名的ID插入到数据库中。
我已经可以使用此查询display与怪物关联的怪物类型:Monster Display
$query = "SELECT Monsters.ID AS `mID`,
Monsters.Name AS `MName`, Monsters.MonsterType_ID,
MonsterType.Name FROM Monsters ";
$query .= "inner join MonsterType ON Monsters.MonsterType_ID = MonsterType.ID
ORDER BY MonsterType.Name ASC";
$result = $mysqli->query($query);
if ($result && $result->num_rows > 0) {
echo "<div class='row'>";
echo "<center>";
echo "<h2>The Monster Database</h2>";
echo "<table>";
echo "<tr><th>Name</th><th>Type</th>
<th></th><th></th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
//Output FirstName and LastName
echo "<td>" .$row["MName"]."</td>";
echo "<td>" .$row["Name"]."</td>";
我只是不知道如何更改Insert查询,以便它在文本字段中接受MonsterType.Name,并在Insert in to Monsters表查询中使用与该MonsterType.Name关联的MonsterType.ID。
当然,理想情况下,我只希望我的Monster Type字段是所有MonsterType.Names的下拉列表,但我在使用HTML code embedded in a PHP block进行下拉字段时遇到问题。
echo '
<div class="row">
<label for="left-label" class="left inline">
<h2>Add a monster</h2>
<form method="POST" action="addMonsters.php">
<p> Monster Name: <input type="text" name="Name">
<p> Hit Points: <input type="text" name="HP">
<p> Mana Points: <input type="text" name="MP">
<p> Armor Class: <input type="text" name="AC">
<p> Monster Type: <input type="text" name="MonsterType_ID">
/////// This didn't work
<p>Monster Type: <select name="Name">
<option></option>
<?php
$query = "SELECT DISTINCT Name FROM MonsterType";
$result = $mysqli->query($query);
if($result && $result->num_rows>=1){
while($row = $result->fetch_assoc()){
echo "<option value = '".$row['Name']."'>".$row['Name']."/option>";
}
}
else {echo "<h2>No query results</h2>";}
?>
</select>
///////////
<input type="submit" name="submit" class="button tiny round" value="Add monster" />
</form>
';
如果有人能解决这个问题,那就更好了。
任何指导都将不胜感激!谢谢您!
编辑:
我已成功使用此代码块填充下拉列表:
echo '<p>Monster Type: <select name="MonsterType_ID">';
echo '<option></option>';
$query = "SELECT DISTINCT ID, Name FROM MonsterType";
$result = $mysqli ->query($query);
if($result && $result->num_rows>=1){
while($row2 = $result->fetch_assoc()){
if($row2['mID'] == $MonsterID){
echo "<option selected value = '".$row2['ID']."'>".$row2['Name']."</option>";
}
else{
echo "<option value = '".$row2['ID']."'>".$row2['Name']."</option>";
}
}
}
else {
echo "<h2>No query results</h2>";
}
echo '</select></p>';
但是,这并没有正确地发布到原始查询,我得到了一条错误消息,我没有填写所有信息。(我在(select name=“MonsterType\u ID”)周围缺少引号)但是,我仍然得到“Error!无法添加“$\u POST[”Name“]。”;所以这意味着查询结果为false。不知道会发生什么,除了MonsterType.ID可能与Monsters.MonsterType\u ID不匹配。
if (isset($_POST["submit"])) {
if( (isset($_POST["Name"]) && $_POST["Name"] !== "") &&
(isset($_POST["HP"]) && $_POST["HP"] !== "") &&
(isset($_POST["MP"]) && $_POST["MP"] !== "") &&
(isset($_POST["AC"]) && $_POST["AC"] !== "") &&
(isset($_POST["MonsterType_ID"]) && $_POST["MonsterType_ID"] !== "") ) {
//STEP 2.
//Create query to insert information that has been posted
$query = "INSERT INTO Monsters ";
$query .= "(Name, HP, MP, AC, MonsterType_ID) ";
$query .= "VALUES (";
$query .= "'".$_POST["Name"]."',";
$query .= "'".$_POST["HP"]."',";
$query .= "'".$_POST["MP"]."',";
$query .= "'".$_POST["AC"]."',";
$query .= "'".$_POST["MonsterType_ID"]."'));";
//$query .= "(SELECT ID FROM MonsterType WHERE Name='".$_POST["MonsterType_ID"]."'));";
$result = $mysqli->query($query);
// Execute query
if($result) {
$_SESSION["message"] = $_POST["Name"]." has been added!";
header("Location: readMonsters.php");
exit;
}
else {
$_SESSION["message"] = "Error! Could not add ".$_POST["Name"]."!";
header("Location: addMonsters.php");
exit;
}
}
else {
$_SESSION["message"] = "Unable to add monster. Fill in all information!";
header("Location: addMonsters.php");
exit;
}
编辑二:一切正常!下拉列表是正确的,我只需要更改查询以接受正确的参数。
$query = "INSERT INTO Monsters ";
$query .= "(Name, HP, MP, AC, MonsterType_ID) ";
$query .= "VALUES (";
$query .= "'".$_POST["Name"]."',";
$query .= "'".$_POST["HP"]."',";
$query .= "'".$_POST["MP"]."',";
$query .= "'".$_POST["AC"]."',";
//$query .= "'".$_POST["MonsterType_ID"]."'));";
$query .= "(SELECT ID FROM MonsterType WHERE ID='".$_POST["MonsterType_ID"]."'));";
$result = $mysqli->query($query);
也就是说,我将(从MonsterType WHERE Name=…)中选择ID改为(从MonsterType WHERE ID=…)中选择ID)。我的下拉列表现在成功工作了。谢谢你尼克的帮助!
最佳答案
简而言之,考虑到您当前的代码结构,请更改这一行:
$query .= "'".$_POST["MonsterType_ID"]."');";
到
$query .= "(SELECT ID FROM MonsterType WHERE Name='".$_POST["MonsterType_ID"]."'));";
这将执行子查询选择,从MonsterType获取提交名称的相应ID。
冗长的回答
这种方法的问题是,如果用户在MonsterType_ID文本框中键入的内容不是有效的MonsterType,例如,他们将“Eraboration”误输入为“Abberation”,则此查询将失败。您尝试生成下拉列表是正确的方法。你会想要这样的东西:
<p>Monster Type: <select name="MonsterType_ID">
<?php
$query = "SELECT ID, Name FROM MonsterType";
$result = $mysqli->query($query);
if ($result) {
while ($row = $result->fetch_assoc()) {
echo "<option value = \"" . $row['ID'] . "\">" . $row['Name'] . "</option>";
}
}
?>
</select>
这将为您提供一个选择,其中选项值(在
$_POST
中传递给PHP的值)是您需要插入到Monsters数据库中的ID。这样,你不需要做任何预处理,如简短的答案(基本上,你现有的代码将按原样工作)。关于php - PHP/MySQL:将外键表插入表1中的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50033617/