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

问题描述 投票:0回答:1

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

单个变量的数据由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>
php mysql arrays ajax
1个回答
0
投票

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

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

应该换成

$data = $_POST['data'];

然后你的代码将工作(除了索引15上的最后一个查询可能会失败,因为$ data的索引中没有数据,你可能想检查一下)。

您的ajax请求正在以标准格式 - urlencoded格式发送数据。尽管您可能在$ .ajax调用中提供了一个JavaScript(注意,而不是JSON!)对象作为data:选项,但jQuery会以静默方式将其转换并编码为标准数据字符串。如果你要为你的ajax设置不同的contentType,例如JSON,然后它会为你转换为JSON,但这不是必需的,无论如何你完成它的方式更简单。

© www.soinside.com 2019 - 2024. All rights reserved.