简体   繁体   English

使用AJAX和PHP将数据数组插入SQL

[英]Insert Array(s) of Data into SQL using AJAX and PHP

I am trying to insert an array(s) of data into SQL using AJAX and PHP. 我正在尝试使用AJAX和PHP将数据数组插入SQL。 The array is built in Javascript and passed into PHP which a SQL statment grabs the data. 该数组是用Javascript构建的,并传递给PHP,SQL语句可以获取数据。 It is supposed to insert the data, but it is not inserting. 应该插入数据,但不插入。

In my PHP, I declared 6 variables and inserted random data to test to see if the query and the data was working; 在我的PHP中,我声明了6个变量并插入了随机数据以测试查询和数据是否正常工作; and it does. 确实如此。 It's when I use the 'SqlArr' for my values that dont insert. 这是当我将'SqlArr'用于不插入的值时。 How do I insert an array of data that i am dynamically pushing into the SQL database using AJAX and PHP? 如何使用AJAX和PHP将要动态推送到SQL数据库的数据插入数组?

Any help is most appreciated. 非常感谢您的帮助。

JavaScript: JavaScript的:

            var cnt=0;  
            Empdata = 'EmpData' + cnt + '';
            temp={EmpId,WeekEnding,DateOccur,JobNum, Customer, HourValue};
            SqlArr.push({Empdata : temp});

PHP PHP

<?php
include_once 'DbConnectPSI.php';
    global $connect;
    global $record3;
    global $myData3;
    global $var1;
    global $var2;
    global $var3;
    global $var4;
    global $var5;
    global $var6;

    $var1=2;
    $var2=2015-09-12;
    $var3=2015-09-09;
    $var4=2;
    $var5=2;
    $var6=55;


    $SqlArr =$_POST['SqlArr'];

    $myData3="Insert Into EmployeeTimesheets  Values('$SqlArr', getDate())";    
    //$myData3="Insert Into EmployeeTimesheets  Values('$var1','$var2','$var3','$var4','$var5','$var6', getDate())";    

    $record3 = odbc_exec($connect, $myData3);


    odbc_close($connect);
    ?>

Data comming from Javascript and into the SqlArr.push 从Javascript到SqlArr.push的数据

EmpId=2, WeekEnding=2015-09-12, DateOccur=2015-09-09, JobNum=2, Customer=2, HourValue=55 EmpId = 2,WeekEnding = 2015-09-12,DateOccur = 2015-09-09,JobNum = 2,Customer = 2,HourValue = 55

` Table structure using Sql Server 2008 R2 `使用Sql Server 2008 R2的表结构

 USE [xx]
    GO

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[EmployeeTimeSheets](
        [PkId] [bigint] IDENTITY(1,1) NOT NULL,
        [EmpId] [int] NULL,
        [WkEnd] [date] NULL,
        [Day] [date] NULL,
        [Title] [nvarchar](20) NULL,
        [Description] [nvarchar](50) NULL,
        [Value] [float] NULL,
        [TimeStamp] [datetime] NULL,
     CONSTRAINT [PK_EmployeeTimeSheets] PRIMARY KEY CLUSTERED 
    (
        [PkId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO

Array being received by PHP using var_dump PHP使用var_dump接收数组

在此处输入图片说明

You can not dump the whole array into SQL with a single interpolation like you are doing on this line: 您不能像在此行中那样通过单个插值将整个数组转储到SQL中:

$myData3="Insert Into EmployeeTimesheets  Values('$SqlArr', getDate())"; 

If you were to look at the value of $myData3 , it would be: 如果要查看$myData3的值,它将是:

Insert Into EmployeeTimesheets Values('Array', getDate()) 插入EmployeeTimesheets值('Array',getDate())

I see that $SqlArr is also two-dimensional, with the first dimension just being a reference to the data that you actually want to work with. 我看到$SqlArr也是二维的,第一维只是对您实际要使用的数据的引用。 So before anything else, you probably want to do this: 因此,在进行其他操作之前,您可能想要执行以下操作:

$SqlArr = $SqlArr[0];

Then instead of trying to interpolate the entire array, you need to reference the individual array items, like this: 然后,您不必引用整个数组,而是需要引用单个数组项,如下所示:

$myData3="Insert Into EmployeeTimesheets  "
    + "Values('$SqlArr[EmpId]', '$SqlArr[WeekEnding]', getDate())"; 

I've only included two of the items in the example, but you would include all of them. 在示例中,我仅包含了两个项目,但是您将包括所有这些项目。

You also should enumerate the columns in the INSERT statement to avoid problems in the future. 您还应该枚举INSERT语句中的列,以避免将来出现问题。 Like this: 像这样:

$myData3="Insert Into EmployeeTimesheets (EmpId, WkEnd) "
    + "Values('$SqlArr[EmpId]', '$SqlArr[WeekEnding]', getDate())"; 

Finally, you really shouldn't put the values directly into the SQL, but should use a parameterized query instead. 最后,您实际上不应该将值直接放入SQL中,而应使用参数化查询。 I typically use PDO for database access, and a parameterized implementation of the insert looks something like this: 我通常使用PDO进行数据库访问,插入的参数化实现如下所示:

$sql = "Insert Into EmployeeTimesheets (EmpId, WkEnd) Values(?, ?, getDate())"; 
$conn = new PDO("connection string goes here");
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $SqlArr['EmpId']);
$stmt->bindParam(2, $SqlArr['WeekEnding']);
$stmt->execute();

You can also use named parameters if you prefer. 如果愿意,还可以使用命名参数。 See the PDO documentation . 请参阅PDO文档

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

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