繁体   English   中英

使用Ajax在php中传递的数组将记录插入到MYSQL数据库表中

[英]Using array passed by Ajax in php to Insert record to MYSQL database table

我有一个SVG元素调用的javascript函数,用于将2个变量和多维数组传递给php文件。 然后,php文件将使用传递的变量(数组和2个变量),使用传递的数据在一个MYSQL表中创建一条记录,在另一个表中创建多个记录。

单个变量的数据由AJAX ok传递,我可以用它成功地在Exercise表中创建一条记录。

数组的数据也将正确无误地传递。 在通过AJAX传递之前,我已经测试过是否有数据-请参见功能代码。

Javascript控制台未显示任何错误。 我相信我已经正确使用JSON来编码和解码数组。

但是,不会使用数组中的数据创建MYSQL记录。 我怀疑我没有正确使用解码后的数组。 只是对为什么可以正确传递一两段数据而不能正确传递数组感到困惑。

任何帮助表示赞赏-查理

请参见代码:Javascript函数

function createexercise(id)
{
    pathways = <?php echo json_encode($myarray) ?>;

    for (var x=1;x<=15;x++)//loops through pathway array and makes each pathway non visible
        {
            document.getElementById(x).style.visibility = "hidden";
        }//for loop scope

    document.getElementById(id).style.visibility="hidden";
    var z=0;
    var SPX=0;
    var SPY=0;
    var EPX=0;
    var EPY=0;

        for(var x=0;x<=newpath.length-1;x++)//cycle through exercise pathways
        {               
            for (var y=0;y<=pathways.length-1; y++)// cycle through ALL pathways possible
            {               
                if(y==0)//if first possible pathway add Start X coordinate to Start Y coordinate
                {
                    pathwayselected=pathwayselected+pathways[y][3]+","+pathways[y][4]+" ";          
                }//if scope

                z=pathways[y][0];
                SPX=pathways[y][3];
                SPY=pathways[y][4];
                EPX=pathways[y][5];
                EPY=pathways[y][6];

                if(z==newpath[x] && y>0)//if first possible pathway add Start X coordinate to Start Y coordinate to End X and Y coordinates
                {
                    var selected = document.getElementById(newpath[z]);
                    pathwayselected=pathwayselected+" L"+SPX+","+SPY+" L"+EPX+","+EPY+" ";
                }//if scope
            }//y loop scope                         
        }//x loop scope

        // Test to show that the "pathways" array is data populated
        for(q=5; q<6; q++)
        {
        alert("Pathway ID element is....:"+pathways[q][0]);
        alert("Pathway Start ID element is....:"+pathways[q][1]);
        alert("Pathway End IDelement is....:"+pathways[q][2]);
        alert("Pathway Start X Coord element is....:"+pathways[q][3]);
        alert("Pathway Start Y Coord element is....:"+pathways[q][4]);
        alert("Pathway End X Coord element is....:"+pathways[q][5]);
        alert("Pathway End Y Coord element is....:"+pathways[q][6]);    
        }

        // Then the Exercise ID and the pathway string that has been built is sent to a PHP file to create a
        // mysql database "Exercise" record and a series of "Exercise Pathway" records

        var exid = parseInt(id);   

        $.ajax({
        type: "POST",
        url: "InsertExercisePathsSecure.php",
        data: {'id': exid, 'PathSelected': pathwayselected,'data' : pathways}, 
        cache: false,

        success: function(data){
            console.log(data);
            alert("OK we are back");
        },      
        error: function(xhr, ajaxOptions, thrownError)
            {   alert("Error code is....:"+xhr.status); }
        });//s.ajax outer scope

    var rect = document.getElementById("start");
    rect.style.visibility="visible";
    var rectt1 = document.getElementById("t1");
    rectt1.style.visibility="visible"

  }//end of function createexercise scope

php文件-InsertExercisePathsSecure.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
</head>

<?php
# Fill our variables to set up environment

$dbname = '???????';
$dbuser = '???????';
$dbpass = '???????';
$dbhost = '???????';

# set the database connection to be used using php function
$conn=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

///////////////////////////////////////////////////
// allocate php variables to $_POST[] data received
///////////////////////////////////////////////////

$EID = $_POST['id'];
$PathData = $_POST['PathSelected'];
$data = json_decode(stripslashes($_POST['data']));

///////////////////////////////////////////////////
// create mysql query using php variables and then execute it
///////////////////////////////////////////////////

mysqli_query($conn,"INSERT INTO ExerciseTable (ExerciseID, PathwayData)
VALUES ('$EID', '$PathData')"); 

///////////////////////////////////////////////////
// loop through received decoded array and allocate php variables to
each element field
// after variable allocation execute mysql process
///////////////////////////////////////////////////    

for ($x=0; $x<15; $x++)
{
    $PID = $data[$x][0];
    $TL = $data[$x][2];
    $EP = $data[$x][3];
    $ROW = $data[$x][4];
    $PS = $data[$x][5];

    mysqli_query($conn, "INSERT INTO ExercisePathwayTable (ExerciseID,
    PathwayID, tablelocator, ExercisePathway, Row, PathwaySequence)
    VALUES('$EID','$PID','$TL','$EP','$ROW','$PS')");

 }
mysqli_close($conn);

?>
</body>
</html>



  [Log]     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1- 
 transitional.dtd"> (SVGDOMCreate.php, line 194)
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  />
    <title></title>
    </head>
    <body>
    </head>

    array(3) {
  ["id"]=>
  string(2) "18"
  ["PathSelected"]=>
  string(52) "M 100,60  L195,60 L330,490 100,60  L330,60 L330,490 "
  ["data"]=>
  array(16) {
    [0]=>
    array(14) {
      [0]=>
      string(1) "1"
      [1]=>
      string(1) "1"
      [2]=>
      string(1) "2"
      [3]=>
      string(3) "100"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "100"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "1"
      ["StartPathwayID"]=>
      string(1) "1"
      ["EndPathwayID"]=>
      string(1) "2"
      ["StartPathwayXCoord"]=>
      string(3) "100"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "100"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [1]=>
    array(14) {
      [0]=>
      string(1) "2"
      [1]=>
      string(1) "1"
      [2]=>
      string(1) "4"
      [3]=>
      string(3) "100"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "195"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "2"
      ["StartPathwayID"]=>
      string(1) "1"
      ["EndPathwayID"]=>
      string(1) "4"
      ["StartPathwayXCoord"]=>
      string(3) "100"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "195"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [2]=>
    array(14) {
      [0]=>
      string(1) "3"
      [1]=>
      string(1) "1"
      [2]=>
      string(1) "6"
      [3]=>
      string(3) "100"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "330"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "3"
      ["StartPathwayID"]=>
      string(1) "1"
      ["EndPathwayID"]=>
      string(1) "6"
      ["StartPathwayXCoord"]=>
      string(3) "100"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "330"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [3]=>
    array(14) {
      [0]=>
      string(1) "4"
      [1]=>
      string(1) "3"
      [2]=>
      string(1) "2"
      [3]=>
      string(3) "195"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "100"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "4"
      ["StartPathwayID"]=>
      string(1) "3"
      ["EndPathwayID"]=>
      string(1) "2"
      ["StartPathwayXCoord"]=>
      string(3) "195"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "100"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [4]=>
    array(14) {
      [0]=>
      string(1) "5"
      [1]=>
      string(1) "3"
      [2]=>
      string(1) "4"
      [3]=>
      string(3) "195"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "195"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "5"
      ["StartPathwayID"]=>
      string(1) "3"
      ["EndPathwayID"]=>
      string(1) "4"
      ["StartPathwayXCoord"]=>
      string(3) "195"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "195"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [5]=>
    array(14) {
      [0]=>
      string(1) "6"
      [1]=>
      string(1) "3"
      [2]=>
      string(1) "6"
      [3]=>
      string(3) "195"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "330"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "6"
      ["StartPathwayID"]=>
      string(1) "3"
      ["EndPathwayID"]=>
      string(1) "6"
      ["StartPathwayXCoord"]=>
      string(3) "195"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "330"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [6]=>
    array(14) {
      [0]=>
      string(1) "7"
      [1]=>
      string(1) "5"
      [2]=>
      string(1) "2"
      [3]=>
      string(3) "330"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "100"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "7"
      ["StartPathwayID"]=>
      string(1) "5"
      ["EndPathwayID"]=>
      string(1) "2"
      ["StartPathwayXCoord"]=>
      string(3) "330"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "100"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [7]=>
    array(14) {
      [0]=>
      string(1) "8"
      [1]=>
      string(1) "5"
      [2]=>
      string(1) "4"
      [3]=>
      string(3) "330"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "195"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "8"
      ["StartPathwayID"]=>
      string(1) "5"
      ["EndPathwayID"]=>
      string(1) "4"
      ["StartPathwayXCoord"]=>
      string(3) "330"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "195"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [8]=>
    array(14) {
      [0]=>
      string(1) "9"
      [1]=>
      string(1) "5"
      [2]=>
      string(1) "6"
      [3]=>
      string(3) "330"
      [4]=>
      string(2) "60"
      [5]=>
      string(3) "330"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(1) "9"
      ["StartPathwayID"]=>
      string(1) "5"
      ["EndPathwayID"]=>
      string(1) "6"
      ["StartPathwayXCoord"]=>
      string(3) "330"
      ["StartPathwayYCoord"]=>
      string(2) "60"
      ["EndPathwayXCoord"]=>
      string(3) "330"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [9]=>
    array(14) {
      [0]=>
      string(2) "10"
      [1]=>
      string(1) "7"
      [2]=>
      string(1) "2"
      [3]=>
      string(2) "65"
      [4]=>
      string(2) "25"
      [5]=>
      string(3) "100"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(2) "10"
      ["StartPathwayID"]=>
      string(1) "7"
      ["EndPathwayID"]=>
      string(1) "2"
      ["StartPathwayXCoord"]=>
      string(2) "65"
      ["StartPathwayYCoord"]=>
      string(2) "25"
      ["EndPathwayXCoord"]=>
      string(3) "100"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [10]=>
    array(14) {
      [0]=>
      string(2) "11"
      [1]=>
      string(1) "7"
      [2]=>
      string(1) "4"
      [3]=>
      string(2) "65"
      [4]=>
      string(2) "25"
      [5]=>
      string(3) "195"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(2) "11"
      ["StartPathwayID"]=>
      string(1) "7"
      ["EndPathwayID"]=>
      string(1) "4"
      ["StartPathwayXCoord"]=>
      string(2) "65"
      ["StartPathwayYCoord"]=>
      string(2) "25"
      ["EndPathwayXCoord"]=>
      string(3) "195"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [11]=>
    array(14) {
      [0]=>
      string(2) "12"
      [1]=>
      string(1) "7"
      [2]=>
      string(1) "6"
      [3]=>
      string(2) "65"
      [4]=>
      string(2) "25"
      [5]=>
      string(3) "330"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(2) "12"
      ["StartPathwayID"]=>
      string(1) "7"
      ["EndPathwayID"]=>
      string(1) "6"
      ["StartPathwayXCoord"]=>
      string(2) "65"
      ["StartPathwayYCoord"]=>
      string(2) "25"
      ["EndPathwayXCoord"]=>
      string(3) "330"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [12]=>
    array(14) {
      [0]=>
      string(2) "13"
      [1]=>
      string(1) "8"
      [2]=>
      string(1) "6"
      [3]=>
      string(3) "360"
      [4]=>
      string(2) "25"
      [5]=>
      string(3) "330"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(2) "13"
      ["StartPathwayID"]=>
      string(1) "8"
      ["EndPathwayID"]=>
      string(1) "6"
      ["StartPathwayXCoord"]=>
      string(3) "360"
      ["StartPathwayYCoord"]=>
      string(2) "25"
      ["EndPathwayXCoord"]=>
      string(3) "330"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [13]=>
    array(14) {
      [0]=>
      string(2) "14"
      [1]=>
      string(1) "8"
      [2]=>
      string(1) "4"
      [3]=>
      string(3) "360"
      [4]=>
      string(2) "25"
      [5]=>
      string(3) "195"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(2) "14"
      ["StartPathwayID"]=>
      string(1) "8"
      ["EndPathwayID"]=>
      string(1) "4"
      ["StartPathwayXCoord"]=>
      string(3) "360"
      ["StartPathwayYCoord"]=>
      string(2) "25"
      ["EndPathwayXCoord"]=>
      string(3) "195"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [14]=>
    array(14) {
      [0]=>
      string(2) "15"
      [1]=>
      string(1) "8"
      [2]=>
      string(1) "2"
      [3]=>
      string(3) "360"
      [4]=>
      string(2) "25"
      [5]=>
      string(3) "100"
      [6]=>
      string(3) "490"
      ["PathwayID"]=>
      string(2) "15"
      ["StartPathwayID"]=>
      string(1) "8"
      ["EndPathwayID"]=>
      string(1) "2"
      ["StartPathwayXCoord"]=>
      string(3) "360"
      ["StartPathwayYCoord"]=>
      string(2) "25"
      ["EndPathwayXCoord"]=>
      string(3) "100"
      ["EndPathwayYCoord"]=>
      string(3) "490"
    }
    [15]=>
    string(5) "false"
  }
}
    </body>
    </html>

我刚刚意识到我一开始就应该意识到的-您没有任何JSON数据。

$data = json_decode(stripslashes($_POST['data']));

应该替换为

$data = $_POST['data'];

然后您的代码将正常工作(除非对索引15的最后一个查询可能会失败,因为该索引的$ data中没有数据,您可能要检查一下)。

您的ajax请求正在以标准的表单编码格式发送数据。 即使您可能在$ .ajax调用中提供了一个JavaScript对象(请注意,不是JSON!)作为data:选项,jQuery仍会默默地将其转换并编码为标准数据字符串。 如果要为ajax设置不同的contentType (例如JSON),则它将为您将其转换为JSON,但这不是必需的,而且无论如何,这样做都比较简单。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM