Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
Php 如何使用多个select从动态生成的行插入MySQL数据?_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Php 如何使用多个select从动态生成的行插入MySQL数据?

Php 如何使用多个select从动态生成的行插入MySQL数据?,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,表C是用户列表。表A是我们有用户名、值的表。在表B中,我们有类似于tableA的视图,因为mytabA_id是tableA中的名称,而multiple_tabA_id是位于多个数组中的这些用户的id 第一个问题是将多个select to DB保存到表A。第二个问题是将行插入到表B取决于表A 我想在表A中动态添加新行。 我有一个源代码可以通过JQuery添加新行。我觉得很好 tableA tabA_id name value -------------------

表C是用户列表。表A是我们有用户名、值的表。在表B中,我们有类似于tableA的视图,因为mytabA_id是tableA中的名称,而multiple_tabA_id是位于多个数组中的这些用户的id

第一个问题是将多个select to DB保存到表A。第二个问题是将行插入到表B取决于表A

我想在表A中动态添加新行。 我有一个源代码可以通过JQuery添加新行。我觉得很好

tableA   tabA_id   name     value         
------------------------------------
Row1       25      nameA    1.25    
Row2       26      nameB    4.85    
Row3       27      nameA    3.25    

tableB   tabA_id   mytabA_id   multiple_tabA_id
--------------------------------------------------
Row1       25         1               1
Row2       25         1               3
Row3       25         1               4
Row4       26         2               1
Row5       26         2               2
Row6       27         1               1
Row7       27         1               2
Row8       27         1               4

tableC   tabC_id   name   
--------------------------
Row1        1      nameA               
Row2        2      nameB              
Row3        3      nameC               
Row4        4      nameD               
表_save_ajax.php

在配置中,只有连接到数据库。这是正确的

function Save(){ 
   var par = $(this).parent().parent(); 

   var tdname = par.children("td:nth-child(2)");
   var tdvalue = par.children("td:nth-child(3)"); 
   var tdmultiple = par.children("td:nth-child(4)"); 

   var name = tdname.children("input[type=text]").val();
   var value = tdvalue.children("input[type=text]").val();
   var multiple = $( "#multiple" ).val() || [];

jQuery.post("table_save_ajax.php", {
   name:name,
   value:value,
   multiple:multiple
   },  function(data, textStatus){
      if(data == 1){
         $('#response').html("Successfull!!");
         $('#response').css('color','green');
         $('td:nth-child(5)').hide();

         tdname.html(tdname.children("input[type=text]").val());
         tdvalue.html(tdvalue.children("input[type=text]").val());
         tdmultiple.html(multiple.join( ", " ));

      }else{
         $('#response').html("Some Error Occurred");
         $('#response').css('color','red');
      }
   });
没有多重选择,一切都是正确的。我想知道如何用多重选择来纠正它。我想知道如何将数据插入tableB。如何从数组中解析此数据并保存到数据库。
谢谢您的回复。

我试过这个,它对我有好处。 它是table_save_ajax.php


你会想要mysql_escape_字符串你的输入$_POST['name'],$_POST['value']等等,或者更好地使用mysqli或PDO并使用准备好的语句/bindValue。好吧,但现在我不想解决代码的安全问题。。。我想解决的功能…当有人不知道答案,请给我投票加。接下来我想上传图片。这对于编写像代码这样的表不是很好。。。谢谢。我不认为这就是投票的目的:如果人们没有回答你的问题,考虑重新表述问题或重新思考你的问题。我假设要插入到tableB中,在插入新行之后,首先需要从tableA中选择id,或者可以使用mysql\u insert\u id。您认为行的id还是多个名称的id?
function Save(){ 
   var par = $(this).parent().parent(); 

   var tdname = par.children("td:nth-child(2)");
   var tdvalue = par.children("td:nth-child(3)"); 
   var tdmultiple = par.children("td:nth-child(4)"); 

   var name = tdname.children("input[type=text]").val();
   var value = tdvalue.children("input[type=text]").val();
   var multiple = $( "#multiple" ).val() || [];

jQuery.post("table_save_ajax.php", {
   name:name,
   value:value,
   multiple:multiple
   },  function(data, textStatus){
      if(data == 1){
         $('#response').html("Successfull!!");
         $('#response').css('color','green');
         $('td:nth-child(5)').hide();

         tdname.html(tdname.children("input[type=text]").val());
         tdvalue.html(tdvalue.children("input[type=text]").val());
         tdmultiple.html(multiple.join( ", " ));

      }else{
         $('#response').html("Some Error Occurred");
         $('#response').css('color','red');
      }
   });
<?php
include("config.php");
$name = $_POST['name'];
$value = $_POST['value'];
$multiple = $_POST['multiple'];
$sql = mysql_query("INSERT INTO tableA (name, value, multiple) VALUES ('$name','$value','$multiple')");

if(mysql_affected_rows()>0){
echo "1";
}else{
echo "2";
}
?>
$sql = mysql_query("INSERT INTO tableA (name, value) VALUES ('$name','$value')");

if($sql){
    $sqlselect = mysql_query("SELECT id FROM tableA ORDER BY id DESC LIMIT 1");
    if(mysql_num_rows($sqlselect) == 1){
      $row=mysql_fetch_array($sqlselect);
      $lastid=$row['id'];

      foreach ($multiple as &$value) {
        $sqlmultiple = mysql_query("INSERT INTO tableB (tabA_id, mytabA_id, multiple_tabA_id) VALUES ('$lastid', '$name', '$value',)");
        if(!$sqlmultiple){
          $sqldelete = mysql_query("DELETE FROM tableA WHERE id = '$lastid'");
        }  
      }
    } 
}