- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在网上搜索了好几天了,但我就是无法弄清楚这一点。
我正在尝试将表单数据插入到 MySQL 数据库中的多行中,但它无法按我想要的方式工作。有谁可以帮助我吗?
我想要做的是将表单发送到两个单独的表。我希望一些信息进入名为“菜肴”的表,其他信息进入名为“成分”的表。发送到“菜肴”表的内容正常工作,“配料”表不包含配料数组。我想要多行的成分,具体取决于您输入的数量。
到目前为止,成分表正在为每个条目在表中创建多行,但它不会在行中输入任何数据...
这是我的 HTML:
<form method="post" action="insertrecipe.php">
<table>
<tr>
<td class="name_of_dish_heading">Name of dish</td>
</tr>
<tr>
<td><input type="text" name="dish_name"></td>
</tr>
<tr>
<td class="table_text">Short description:</td>
</tr>
<tr>
<td><textarea name="dish_short_description" rows="3" cols="45"></textarea></td>
</tr>
</table>
<table>
<tr>
<td class="table_text">Ingredient:</td>
<td class="table_text">Amount:</td>
<td class="table_text">Type:</td>
</tr>
<tr>
<td><input type="text" name="ingredient[]"></td>
<td><input type="text" name="ingred_amount[]" size="5"></td>
<td>
<select name="ingred_amount_type[]">
<option name="Milliliter" value="Milliliter">Milliliter (ML)</option>
<option name="Centiliter" value="Centiliter">Centiliter (CL)</option>
<option name="Deciliter" value="Deciliter">Deciliter (DL)</option>
<option name="Liter" value="Liter">Liter (L)</option>
</select>
</td>
</tr>
<tr>
<td class="table_text">Ingredient:</td>
<td class="table_text">Amount:</td>
<td class="table_text">Type:</td>
</tr>
<tr>
<td><input type="text" name="ingredient[]"></td>
<td><input type="text" name="ingred_amount[]" size="5"></td>
<td>
<select name="ingred_amount_type[]">
<option name="Milliliter" value="Milliliter">Milliliter (ML)</option>
<option name="Centiliter" value="Centiliter">Centiliter (CL)</option>
<option name="Deciliter" value="Deciliter">Deciliter (DL)</option>
<option name="Liter" value="Liter">Liter (L)</option>
</select>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Add recipe">
</form>
这是我的 PHP:
<?php
require_once('config.php');
// Connect to the database
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dish_name = mysqli_real_escape_string($con, $_POST['dish_name']);
$dish_short_description = mysqli_real_escape_string($con, $_POST['dish_short_description']);
$sql1="INSERT INTO dishes (dish_name, dish_short_description)
VALUES ('$dish_name', '$dish_short_description')";
if (!mysqli_query($con,$sql1)) {
die('[Error: '.__LINE__.']: '.mysqli_error($con));
}
else {
echo "Added to the database.<br /><br />";
}
foreach($_POST['ingredient'] as $row=>$ingred) {
$ingredient = mysqli_real_escape_string($ingred);
$ingred_amount = mysqli_real_escape_string($_POST['ingred_amount'][$row]);
$ingred_amount_type = mysqli_real_escape_string($_POST['ingred_amount_type'][$row]);
$query = "INSERT INTO ingredients (ingredient, ingred_amount, ingred_amount_type) VALUES ('$ingredient', '$ingred_amount', '$ingred_amount_type')";
if (!mysqli_query($con,$query)) {
die('[Error: '.__LINE__.']: '.mysqli_error($con));
}
else {
echo "Added to the database.";
}
}
mysqli_close($con);
?>
非常感谢任何帮助!
最佳答案
设置值时缺少$conn
。
mysqli_real_escape_string ();
需要两个参数。一是连接
第一个是$connection
,第二个是您想要转义的string
。
这就是为什么你无法插入任何东西。即使您的查询没问题。
在设置值时尝试此代码。
$ingredient = mysqli_real_escape_string($con,$ingred); //added $con in every line
$ingred_amount = mysqli_real_escape_string($con,$_POST['ingred_amount'][$row]);
$ingred_amount_type = mysqli_real_escape_string($con,$_POST['ingred_amount_type'][$row]);
关于php - 使用 PHP 通过表单向 MySQL 添加多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439648/
一个雇主有几个雇员,因此雇员与他们的雇主有(多对一)关系。假设我们忽略了从雇主到雇员的反向关系(一对多),那么从雇员的角度来看,这可以表示为一对一的关系!? 最佳答案 乍一看,它可能看起来如此 - 使
因此,我研究了neo4j,由于它的数据模型可能非常适合我的项目,因此我可能会在下一个项目中使用它。我浏览了文档,但仍然需要这个问题的答案: 我可以将关系设置为单向吗? 看来neo4j人们喜欢看电影,所
我在用: Spring Boot Spring Data JPA Spring Data Rest hibernate 嵌入式H2数据库 我正在尝试定义 2 个类,First和 Second ,这样就
给定以下代码 public class Course { @Id @GeneratedValue private Long id; private String nam
我的帐户和组之间存在多对多关系。组表的数据已预先填充,无需添加其他组。 使用 Hibernate,我的愿望是: 将帐户添加到帐户表, 向 account_group 表添加一条记录,并且 不向组表中添
我有一个 SKSpriteNode 作为球,它的所有 SKPhysicsBody 属性都可以向各个方向移动。我现在想要的是使其单向(仅朝它之前未移动到的方向移动,并且不返回到它已移动的路径)。目前我对
我已经看到多个关于此功能可用于蓝牙 4.0/LE 的引用资料,但我不确定从哪里开始或如何实现它。 明确地说,我指的是真正的无连接,只有一台设备上有一个接收器,另一台设备上有一个发射器。 (或者,更具体
给定以下代码 public class Course { @Id @GeneratedValue private Long id; private String nam
我在删除单向 OneToMany 关系中的所有者对象时遇到问题。 相关的部分是我有三个对象:Acronym、Expansion 和 Tag。 Acronym 与 Expansion 具有单向 OneT
我已经基于 this 在 JavaScript 中实现了一个 ElGamal 方案(代码很糟糕,只是想快速测试一下)解释。 var forge = require('node-forge'); var
我在我的模型中应用领域驱动设计聚合模式。我遵循常见的 DDD 聚合模式示例:Order 是管理一个或多个 LineItem 的根聚合。在我在网络上找到的许多聚合模式示例中,LineItem 应该不知道
这是我的场景, 我有 SubForum 类,它包含一组主持人,该集的每个实体 int 都是 User 类。 这是子论坛的映射
我想保存/保留一个实体(父实体),该实体(父实体)具有仅由父对象包含的唯一子对象。一切正常,直到出现重复的 child ,在这里我得到以下异常: Caused by: com.mysql.jdbc.e
这是我为汉诺塔问题编写的 Python 代码,其中塔必须从左桩转移到中间桩,使用右桩作为备用: def hanoi(n, origin = "1", destination = "2", spare
我有几个单向 JPA2 @OnetoMany 关系的失败案例下面是代码片段 @Entity @Table(name="CUSTOMER") @Access(AccessType.FIELD) publ
我想构建 Hibernate OneToMany 关系,其中父级有一个复合主键,子级有一个主键(hibernate 自动生成)。以下是我的工作示例代码: class Parent{ @Embe
我的项目中有以下实体: AccountGroup AccountItem AccountSegment 具有以下关系: AccountGroup有List AccountItem 有 List 一切正
是否可以使用自动布局和界面生成器进行单向尺寸匹配? 例如,我可能有两个标签。我不希望标签 A 大于标签 B,我希望 B 具有其固有大小。但使用“匹配高度/宽度”可能会导致大量文本增加 A 的大小,从而
尝试使用 Weblogic 11g 设置单向 SSL 并遇到问题。我有一个自定义身份(我自己的证书),但我只想执行单向 SSL,那么为什么我需要指定信任 keystore ? (我没有使用信任,因为我
注意:这不是 this 的副本问题,因为我了解您何时会使用单向 channel 。我一直这样做。我的问题是为什么这个程序是有效的: func main() { ch := make(chan<
我是一名优秀的程序员,十分优秀!