Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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
无法将角度数组列表传输到php脚本以插入mysql表_Php_Mysql_Angular - Fatal编程技术网

无法将角度数组列表传输到php脚本以插入mysql表

无法将角度数组列表传输到php脚本以插入mysql表,php,mysql,angular,Php,Mysql,Angular,从尝试使用php脚本在mysql数据库表中插入excel工作表数据开始。我在angular安装了xlsx,然后试图阅读xlsx表 下面是我的候选PloadComponent.ts课程 import * as XLSX from 'xlsx'; export class CandidateuploadComponent implements OnInit { constructor(private userSvc: AddUsersService,private route: Router)

从尝试使用php脚本在mysql数据库表中插入excel工作表数据开始。我在angular安装了
xlsx
,然后试图阅读xlsx表

下面是我的候选PloadComponent.ts课程

import * as XLSX from 'xlsx'; 
export class CandidateuploadComponent implements OnInit {

constructor(private userSvc: AddUsersService,private route: Router) { }

ngOnInit() {
}
title = 'XlsRead';
file:File
arrayBuffer:any
filelist:any

addfile(event)     
{    
  this.file= event.target.files[0];     
  let fileReader = new FileReader();    
  fileReader.readAsArrayBuffer(this.file);     
  fileReader.onload = (e) => {    
  this.arrayBuffer = fileReader.result;    
  var data = new Uint8Array(this.arrayBuffer);    
  var arr = new Array();    
  for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);    
  var bstr = arr.join("");    
  var workbook = XLSX.read(bstr, {type:"binary"});    
  var first_sheet_name = workbook.SheetNames[0];    
  var worksheet = workbook.Sheets[first_sheet_name];    
  console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));    
        var arraylist = XLSX.utils.sheet_to_json(worksheet,{raw:true});     
        this.filelist = [];    
        let usersJson: any[] = arraylist;
 // Sends the form data to the service
  this.userSvc.sendFormData(XLSX.utils.sheet_to_json(worksheet,{raw:true})).subscribe(
  response => console.log("Success! ", response)
  // error => console.error("Error: ", error)
  );
       
  }    

  }       

  }
服务等级

sendFormData(userData) {
console.log('hi');
console.log(userData);
return this.http.post("http://localhost:8088/post_candidateDetails.php", userData);
}
Belwo是php.script

<?php
header("Access-Control-Allow-Origin: http://localhost:4200");
header("Access-Control-Allow-Credentials: true ");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: X-Custom-Header, Origin, Content-Type , Authorisation , X-Requested-With");
header("Content-Type: application/json; charset=UTF-8 ");
$json = file_get_contents('php://input');
$decoded = json_decode($json, true);
$tab = $decoded['tab'];
function conn() {
  $dbhost = "**********";
  $user = "*****";
  $pass = "*****";
  $db = "*********";
  $conn = new PDO('mysql:host=******;dbname=******', $user, $pass);
  return $conn;
}
$db = conn();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);                  
$p = $db->prepare("INSERT INTO CandidateDetails(EmployeeId, FirstName, LastName, Mobile,Email)VALUES(:EmployeeId,:FirstName,:LastName,:Mobile,:Email)");
                       
foreach ($tab as $item) {
  $p->execute($item);
}
echo json_encode(true);

?>
Olivier建议在浏览器中输出错误:

    Array
    (
        [0] => Array
            (
                [Employeeid] => 1
                [FirstName] => fn1
                [LastName] => ln1
                [Mobile] => 123456789
                [Email] => Email1
            )

        [1] => Array
            (
                [Employeeid] => 2
                [FirstName] => fn2
                [LastName] => ln2
                [Mobile] => 123456790
                [Email] => Email2
            )

        [2] => Array
            (
                [Employeeid] => 3
                [FirstName] => fn3
                [LastName] => ln3
                [Mobile] => 123456791
                [Email] => Email3
            )

        [3] => Array
            (
                [Employeeid] => 4
                [FirstName] => fn4
                [LastName] => ln4
                [Mobile] => 123456792
                [Email] => Email4
            )

    )
    <br />
    <b>Fatal error</b>:  Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\post_candidateDetails.php:27
    Stack trace:
    #0 C:\xampp\htdocs\post_candidateDetails.php(27): PDOStatement-&gt;execute(Array)
    #1 {main}
      thrown in <b>C:\xampp\htdocs\post_candidateDetails.php</b> on line <b>27</b><br />
数组
(
[0]=>阵列
(
[Employeeid]=>1
[名字]=>fn1
[姓氏]=>ln1
[手机]=>123456789
[电子邮件]=>电子邮件1
)
[1] =>阵列
(
[Employeeid]=>2
[名字]=>fn2
[姓氏]=>ln2
[手机]=>123456790
[电子邮件]=>电子邮件2
)
[2] =>阵列
(
[员工ID]=>3
[名字]=>fn3
[姓氏]=>ln3
[手机]=>123456791
[电子邮件]=>电子邮件3
)
[3] =>阵列
(
[Employeeid]=>4
[名字]=>fn4
[姓氏]=>ln4
[手机]=>123456792
[电子邮件]=>电子邮件4
)
)

致命错误:未捕获PDOException:SQLSTATE[HY093]:无效参数编号:C:\xampp\htdocs\post_candidateDetails.php:27中未定义参数 堆栈跟踪: #0 C:\xampp\htdocs\post_candidateDetails.php(27):PDO语句执行(数组) #1{main} 在第27行的C:\xampp\htdocs\post_candidateDetails.php中抛出

消息说JSON数据中没有
选项卡
项,这是完全正常的,因为您不发送这样的项

真正的问题是,您复制/粘贴了一些代码,却没有真正理解它的功能。你从一个说法语的人那里获取代码,法语单词“array”是“tableau”,在程序中通常缩写为“tab”。因此,他显然将数组存储在
选项卡
项中

但那不是你正在做的。将数组直接转换为JSON,这意味着
$decoded
已经是数组。因此,您应该这样做:

$decoded = json_decode($json, true);
foreach ($decoded as $item) {
    $p->bindValue(':EmployeeId', $item['Employeeid'], PDO::PARAM_INT);
    $p->bindValue(':FirstName', $item['FirstName'], PDO::PARAM_STR);
    $p->bindValue(':LastName', $item['LastName'], PDO::PARAM_STR);
    $p->bindValue(':Mobile', $item['Mobile'], PDO::PARAM_STR);
    $p->bindValue(':Email', $item['Email'], PDO::PARAM_STR);
    $p->execute();
}

信息非常清楚。您的JSON数据中没有
选项卡
项。那么怎么了,如何实现。@Olivier是否有任何特定的理由在客户端读取excel文件而不是在服务器端读取?也不要在public forumscan上发布您的敏感数据,如DB凭据。您添加了完整的答案,因为几乎没有混淆,而且我是从angular正确发送的@Olivier从您的屏幕截图中,Angular发送一个数组(包含4个元素)。你不需要在这方面做任何改变。您应该只更改PHP脚本(删除
$tab
)。
警告:在第40行的C:\xampp\htdocs\post_candidateDetails.PHP中为foreach()提供的参数无效
true。。。。删除此行后,$tab=$decoded['tab']@Olivier您是否也更改了foreach的
foreach
行?
致命错误:未捕获PDOException:SQLSTATE[HY093]:无效参数号:参数未在C:\xampp\htdocs\post#candidateDetails.php中定义。43堆栈跟踪:#0 C:\xampp\htdocs\post#candidateDetails.php(43):PDOStatement执行(数组)#1{main}在第43行的C:\xampp\htdocs\post_candidateDetails.php中抛出
$decoded = json_decode($json, true);
foreach ($decoded as $item) {
    $p->bindValue(':EmployeeId', $item['Employeeid'], PDO::PARAM_INT);
    $p->bindValue(':FirstName', $item['FirstName'], PDO::PARAM_STR);
    $p->bindValue(':LastName', $item['LastName'], PDO::PARAM_STR);
    $p->bindValue(':Mobile', $item['Mobile'], PDO::PARAM_STR);
    $p->bindValue(':Email', $item['Email'], PDO::PARAM_STR);
    $p->execute();
}