Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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传递到php并通过准备好的语句将其插入mysql数据库时出现问题_Javascript_Php_Jquery_Mysql_Prepared Statement - Fatal编程技术网

将关联数组从javascript传递到php并通过准备好的语句将其插入mysql数据库时出现问题

将关联数组从javascript传递到php并通过准备好的语句将其插入mysql数据库时出现问题,javascript,php,jquery,mysql,prepared-statement,Javascript,Php,Jquery,Mysql,Prepared Statement,只需单击“插入数据”按钮,就会看到一个警告框。这是一个关联数组。这些是我想要传递到php文件中的值 在php文件中,我得到一个Post值,其中包含从ajax数据传递的所有数据。我用json_decode解码了它。现在,数据被提取为stdClass类型的php数组。我现在使用prepared语句通过for循环语句插入所有php数组 使用Xdebug时,箭头会停在php文件的for循环内。在那之后,没有任何东西被插入到我的数据库中。我还注意到,在xdebug上对php上的“count($value)

只需单击“插入数据”按钮,就会看到一个警告框。这是一个关联数组。这些是我想要传递到php文件中的值

在php文件中,我得到一个Post值,其中包含从ajax数据传递的所有数据。我用json_decode解码了它。现在,数据被提取为stdClass类型的php数组。我现在使用prepared语句通过for循环语句插入所有php数组

使用Xdebug时,箭头会停在php文件的for循环内。在那之后,没有任何东西被插入到我的数据库中。我还注意到,在xdebug上对php上的“count($value)”求值时,它返回1而不是3。在XDEBUG中计算$value[0]->fname也会返回一个错误

sample.js

$('#ajax').click(function() {
        var values = $('#mytable tbody tr').map(function() {
            return {
                fname : $('td:eq(0)',this).text(),
                lname : $('td:eq(1)',this).text(),
                point : parseInt($('td:eq(2)',this).text())
            }
        }); 
        var valuesDebug = "";
        for (var i = 0; i < values.length; i++)
        {
            valuesDebug += " " + values[i]["fname"] + " " + values[i]["lname"] + " " + values[i]["point"] + "\n";
        }
        alert(valuesDebug);
        var valueStringed = JSON.stringify(values);
        $.ajax({
            "type":"POST",
            "url":"insertData.php",
            "data":{value : valueStringed},
            "success":function(data){
                alert('Done inserting the current table values');
            }
        });
    });
$('#ajax')。单击(函数(){
var值=$('#mytable tbody tr').map(函数(){
返回{
fname:$('td:eq(0'),this).text(),
lname:$('td:eq(1)”,this).text(),
点:parseInt($('td:eq(2)”,this).text()
}
}); 
var值debug=“”;
对于(变量i=0;i
insertData.php

<?php

    if (isset($_POST['value']))
    {
        $value = json_decode(stripslashes($_POST['value']));
    }
    $mysqli = new mysqli("localhost","root","password","test");
    if($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") "     . $mysqli->connect_error;  
    }

    $stmt = $mysqli->prepare("INSERT INTO team VALUES (NULL,?, ?, ?)"); //NULL is auto increment primary key id
    $stmt->bind_param('ssi', $fname, $lname, $point);

    if (count($value) > 0)
    {
        for( $i = 0 ;$i < count($value);$i++){
            $fname = $value[$i]->fname;
            $lname = $value[$i]->lname;
            $point = $value[$i]->point;
            $stmt->execute();
        }
    }
    $stmt->close();
    $mysqli->close();
?> 

您需要做的事情一目了然:

$stmt->bind_param('ssi', $fname, $lname, $point);
in您的for循环是您在循环中定义它们的,如果您按照上面的方式执行,那么变量是未定义的

因此,请尝试:

$stmt = $mysqli->prepare("INSERT INTO team VALUES (NULL,?, ?, ?)");

if (count($value) > 0)
{
    for( $i = 0 ;$i < count($value);$i++){

        $fname = $value[$i]->fname;
        $lname = $value[$i]->lname;
        $point = $value[$i]->point;

        $stmt->bind_param('ssi', $fname, $lname, $point);
        $stmt->execute();

    }
}
$stmt=$mysqli->prepare(“插入到团队值中(NULL、、、?)”;
如果(计数($value)>0)
{
对于($i=0;$ifname;
$lname=$value[$i]->lname;
$point=$value[$i]->point;
$stmt->bind_参数('ssi',$fname,$lname,$point);
$stmt->execute();
}
}
此代码现在可以工作了

请查看我的原始javascript代码。 在这一部分中,我似乎添加了一个数组valuesDebug,并通过push方法从values数组复制了内容。我不知道这到底是怎么回事。有没有办法把它缩短

顺便说一句,第一行是要转换为values数组的表的行单元格内容

    var values = $('#mytable tbody tr').map(function() {
        return {
            fname : $('td:eq(0)',this).text(),
            lname : $('td:eq(1)',this).text(),
            point : parseInt($('td:eq(2)',this).text())
        }
    });         
   var valuesDebug = [];
    for (var i = 0; i < values.length; i++)
    {
        valuesDebug.push({fname: values[i]["fname"],lname: values[i]["lname"],point: values[i]["point"]});
    }

    var valueStringed = JSON.stringify(valuesDebug);
    // passes valueStringed into ajax ...
var values=$('#mytable tbody tr').map(函数(){
返回{
fname:$('td:eq(0'),this).text(),
lname:$('td:eq(1)”,this).text(),
点:parseInt($('td:eq(2)”,this).text()
}
});         
var VALUESDBUG=[];
对于(变量i=0;i
print\r($\u post)的输出可以发布吗?
(