Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
如何使用ajax和php将多数组保存到数据库_Php_Jquery_Html_Ajax_Pdo - Fatal编程技术网

如何使用ajax和php将多数组保存到数据库

如何使用ajax和php将多数组保存到数据库,php,jquery,html,ajax,pdo,Php,Jquery,Html,Ajax,Pdo,我想将从Ajax传递到PHP的数组保存到表的不同列中 以下是Ajax发送给PHP的数组: 单击“保存”按钮后,我会在浏览器网络标题中看到这一点 表格数据: tableArray[0][]:awdawd tableArray[0][]:awdawd tableArray[0][]:Male tableArray[0][]:<button class='delete'>Delete</button> tableArray[1][]:awdaw tableArray[1][]:

我想将从Ajax传递到PHP的数组保存到表的不同列中

以下是Ajax发送给PHP的数组:

单击“保存”按钮后,我会在浏览器网络标题中看到这一点

表格数据:

tableArray[0][]:awdawd
tableArray[0][]:awdawd
tableArray[0][]:Male
tableArray[0][]:<button class='delete'>Delete</button>
tableArray[1][]:awdaw
tableArray[1][]:awdwa
tableArray[1][]:Female
tableArray[1][]:<button class='delete'>Delete</button>
saveTable.php

<?php
    error_reporting(-1);
    ini_set('display_errors', 'On');

    $host = "localhost";
    $user = "root";
    $pass = "";
    $db = "test";

    $dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
    $dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $tableArray = $_REQUEST['tableArray'];

    foreach( $tableArray As $v){
    $sql = "INSERT INTO viewTables (name, age, gender, action) VALUES ('$v[0]','$[1]','$[2]','$[3]')";
    $query = $dbc->prepare($sql);
    $query->execute();
    }
?>

应该是

$sql = "INSERT INTO viewTables (name, age, gender, action) VALUES ('$v[0]','$v[1]','$v[2]','$v[3]')";  // Added array name for the last three values

@萨迪克指出了这个问题,这只是一个打字错误。但是,我建议您以更好的方式使用准备好的语句,只准备一次语句,并使用参数来防止SQL注入

$sql = "INSERT INTO viewTables (name, age, gender, action) VALUES (:name, :age, :gender, :action)";
try {
    $sth = $dbc->prepare($sql);
    foreach( $tableArray As $v){
        // bind parameter values
        $sth->bindValue(':name', $v[0], PDO::PARAM_STR); 
        $sth->bindValue(':age', $v[1], PDO::PARAM_STR);
        $sth->bindValue(':gender', $v[2], PDO::PARAM_STR);
        $sth->bindValue(':action', $v[3], PDO::PARAM_STR);      
        $sth->execute();
    }
} catch (PDOException $e) {
    // something went wrong
    // log an error or whatever
}
$sql = "INSERT INTO viewTables (name, age, gender, action) VALUES ('$v[0]','$v[1]','$v[2]','$v[3]')";  // Added array name for the last three values
$sql = "INSERT INTO viewTables (name, age, gender, action) VALUES (:name, :age, :gender, :action)";
try {
    $sth = $dbc->prepare($sql);
    foreach( $tableArray As $v){
        // bind parameter values
        $sth->bindValue(':name', $v[0], PDO::PARAM_STR); 
        $sth->bindValue(':age', $v[1], PDO::PARAM_STR);
        $sth->bindValue(':gender', $v[2], PDO::PARAM_STR);
        $sth->bindValue(':action', $v[3], PDO::PARAM_STR);      
        $sth->execute();
    }
} catch (PDOException $e) {
    // something went wrong
    // log an error or whatever
}