Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在mysql表中使用foreach循环插入一个静态值和多个动态值?_Javascript_Php_Jquery_Mysql - Fatal编程技术网

Javascript 如何在mysql表中使用foreach循环插入一个静态值和多个动态值?

Javascript 如何在mysql表中使用foreach循环插入一个静态值和多个动态值?,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,在我的表单中,我有一个名为Product的字段和一个名为addnew的按钮,在每次单击时添加两个名为Weight和单价的字段。现在,如何使用php在mysql表中插入此表单的值?如何循环重量和单价值的每个输入的产品值 这是我试图实现的一个例子:产品有一个值Rice和两组Weight和Unit price值,分别为10、50和20、45。当可用选项中有两组输入时,php将Rice循环2次,而sql将INSERT值插入mysql表(结构如下所述),那么php部分会是什么 MySQL表结构为: CR

在我的表单中,我有一个名为
Product
的字段和一个名为
addnew
的按钮,在每次单击时添加两个名为
Weight
单价的字段。现在,如何使用
php
在mysql表中插入此表单的值?如何循环
重量
单价
值的每个输入的
产品

这是我试图实现的一个例子:产品有一个值
Rice
和两组
Weight
Unit price
值,分别为
10、50和
20、45。当
可用选项中有两组输入时,
php
Rice
循环2次,而
sql
INSERT
值插入mysql表(结构如下所述),那么
php
部分会是什么

MySQL表结构为:

CREATE TABLE IF NOT EXISTS `product_table` (
  `ID` int(11) NOT NULL,
  `product_name` varchar(30) NOT NULL,
  `weight` int(10) NOT NULL,
  `price` int(10) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
这里,ID是
主键
,设置为
自动增量

这是表单的
jquery
html
部分:
$(文档).ready(函数(){
变量包装=$(“.input_fields_wrap”);
var计数=0;
$('p#添加_字段')。单击(函数(e){
e、 预防默认值();
计数+=1;
$(“#容器”)。追加(
“\n\
重量\n\
单价\n\

" ); }); $(包装器)。在(“单击”、“.remove_字段”上,函数(e){ e、 预防默认值(); $(this.parent('div').remove(); x--; }) });

产品
可用选项: 添加新字段

保存到数据库
您好,您可以在PHP中使用以下代码

<?php
for($ri=0; $ri<count($_POST["weight"]); $ri++){
   $sql = "INSERT INTO product_table (product_name, weight, price) VALUES ('".$_POST["product"]."', '".$_POST["weight"][$ri]."', '".$_POST["price"][$ri]."')";

   //Execute $sql in here with your connection 
}

您好,您可以在PHP中使用以下代码

<?php
for($ri=0; $ri<count($_POST["weight"]); $ri++){
   $sql = "INSERT INTO product_table (product_name, weight, price) VALUES ('".$_POST["product"]."', '".$_POST["weight"][$ri]."', '".$_POST["price"][$ri]."')";

   //Execute $sql in here with your connection 
}

您可以使用我在此类问题上使用的方法

将您的表格更新为:-

<form action="" method="POST">
   <input type="hidden" name="count" id="count" value="0">
   <label>Product</label><input type="text" id="product" name="product"><br>
   <div id="container" class="input_fields_wrap">
      <label>Available options:</label>
      <div>
         <p id="add_field"><button type="button" href="#"><span>Add new</span></button></p>
      </div>
   </div>
   <button type="submit" name="btnSubmit">Save to database</button>
</form>
因此,现在每次添加字段时,它们的名称都会变成
weight\u countVal
price\u countVal
,可以通过后端轻松访问

您的后端php可能如下所示:-

$totalRows = $_POST['count']; //the hidden variable keeping count of total rows

for($i=1;$i<=$totalRows;$i++)
{
  $weight = $_POST['weight_'.$i];
  $price = $_POST['price_'.$i];
  //run your insert commands here 
}
$totalRows=$\u POST['count']//保留总行数的隐藏变量

对于($i=1;$i您可以使用我在此类问题上所使用的

将您的表格更新为:-

<form action="" method="POST">
   <input type="hidden" name="count" id="count" value="0">
   <label>Product</label><input type="text" id="product" name="product"><br>
   <div id="container" class="input_fields_wrap">
      <label>Available options:</label>
      <div>
         <p id="add_field"><button type="button" href="#"><span>Add new</span></button></p>
      </div>
   </div>
   <button type="submit" name="btnSubmit">Save to database</button>
</form>
因此,现在每次添加字段时,它们的名称都会变成
weight\u countVal
price\u countVal
,可以通过后端轻松访问

您的后端php可能如下所示:-

$totalRows = $_POST['count']; //the hidden variable keeping count of total rows

for($i=1;$i<=$totalRows;$i++)
{
  $weight = $_POST['weight_'.$i];
  $price = $_POST['price_'.$i];
  //run your insert commands here 
}
$totalRows=$\u POST['count'];//保存总行数的隐藏变量


对于($i=1;$i您必须创建一个php页面,以使用mysqli函数和SQL在数据库中插入行。页面名称必须在形式上的操作值中。我正在询问php将是什么样的示例。谢谢。页面加载时,默认情况下有多少组
weight
price
行?默认情况下没有行e> 权重
价格
行存在。这是一个示例。@Akshayy您必须创建一个php页面,使用mysqli函数和SQL将该行插入数据库中。页面名称必须在形式上我询问的php示例的动作值中。谢谢。当页面加载时,有多少组
权重
price
行默认存在?默认情况下,不存在一组
weight
price
行。这是一个示例。@AkshayHi,如果连接结构是:
谢谢。感谢您的努力,但它会在
行中返回一个
解析错误:语法错误,意外“;”
for($ri=0;$riBecause of I found the missing bracket”)“for($ri=0;$riWow!它解决了错误,但它没有将行
product\u name
中的product name值插入bd。它保留为空。请检查
这一点,您的元素名称是“product”,因此在这里的$\u POST[“product”]中应该是相同的名称。如果您找不到问题,请打印_r($\u POST)并将结果发送给我。您好,如果连接结构为:
谢谢。感谢您的努力,但它会在
for($ri=0;$riBecause of I found the missing方括号“)的行中返回一个
解析错误:语法错误,意外“;”
($ri=0;$riWow!它解决了错误,但没有将
product\u name
行中的product name值插入bd。它保留为空。请检查
这一点,因为您的元素名称是“product”,因此在这里$\u POST[“product”]中它应该是相同的名称。如果找不到问题,请打印($\u POST)并将结果发送给我。哇!这可能是另一个有趣的解决方案。非常感谢您的努力。我非常感谢您的帮助。:)哇!这可能是另一个有趣的解决方案。非常感谢您的努力。我非常感谢您的帮助。:)