我无法将数据插入数据库

时间:2019-05-31 03:37:08

标签: php angular

这是我的代码,我无法将数据添加到数据库中。请帮我。我不知道该如何解决。我求你们。拜托。

ts

import { Component, On Init } from '@angular/core';
import { Location } from '@angular/common';
import { FormBuilder,FormGroup, FormControl, Validators } from '@angular/forms';
import { EdiTradingPartnerService } from 'src/app/services';
import {Router} from "@angular/router";
import {Md5} from 'ts-md5/dist/md5';
@Component({
  selector: 'app-add-edi-tp',
  templateUrl: './add-edi-tp.component.html',
  styleUrls: ['./add-edi-tp.component.css']
})
export class AddEdiTpComponent implements OnInit {

    addForm:FormGroup;
    tp_name:string;
    sender_id:string;
    receiver_id:string;
    tp_location:string;
    flag:string;
  creation_date:string;
    created_by:string;
    update_date:string;
    updated_by:string;
    tmx_interchange_id:string;
    submitted:boolean=false;
    error:boolean=false;


  constructor(private ediTradingPartnerService:EdiTradingPartnerService,private formBuilder:FormBuilder, private router:Router) {

      this.tp_name="";
      this.sender_id="";
      this.receiver_id="";
      this.tp_location="";
      this.flag="";
      this.creation_date="",
      this.created_by="",
      this.update_date="",
      this.updated_by="",
      this.tmx_interchange_id=""
   }

  ngOnInit() {

        this.addForm = this.formBuilder.group({

            tp_name: ['', Validators.required],
            sender_id: ['', Validators.required],
            receiver_id: ['', Validators.required],
            tp_location: ['', Validators.required],
            flag: ['', Validators.required],
            creation_date: ['', Validators.required],
            created_by: ['', Validators.required],
            update_date: ['', Validators.required],
            updated_by: ['', Validators.required],
            tmx_interchange_id: ['', Validators.required],

        });
  }
  add(post){
    let currentUser = localStorage.getItem("currentUser");
    this.submitted=true;
    console.log("1");
    this.tp_name=post.tp_name;
    this.sender_id=post.sender_id;
    this.receiver_id=post.receiver_id;
    this.tp_location=post.tp_location;
    this.flag=post.flag;
    this.created_by= currentUser;
    this.tmx_interchange_id=post.tmx_interchange_id;

    this.ediTradingPartnerService.add(this.tp_name,this.sender_id,this.receiver_id,this.tp_location,this.flag,this.created_by,this.tmx_interchange_id).subscribe(users=>{
        console.log(this.tp_name,this.sender_id,this.receiver_id,this.tp_location,this.flag,this.created_by,this.tmx_interchange_id);
      console.log("2");
      alert("Succesfully Registered");

       this.router.navigate(['ediTradingPartners']); 

    },
    error=>{
      console.log("3");
      this.error=!this.error;
      console.log(error);

    }
    );
  }

}

服务

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Response, Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { catchError, map, tap } from 'rxjs/operators';

import { EdiTradingPartner } from '../models';

// const httpOptions = {
//  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
// };

@Injectable()
export class EdiTradingPartnerService {

  private ediTradingPartnerUrl = 'http://cbintranet3/edireport/editracker-api/index.php/edi_trading_partner';

  constructor(private http: HttpClient,
               private h1: Http) { }

  // Get all EDI Trading Partners
  getEdiTradingPartners (): Observable<EdiTradingPartner[]> {

    return this.http.get<EdiTradingPartner[]>(this.ediTradingPartnerUrl);
  }

  getTPById(id: number){

     return this.h1.get(this.ediTradingPartnerUrl + '/select/' + id)
     .map((data: Response) => {
       return data.json();
     })
   }

  // Add an EDI Trading Partner
  add(tp_name,sender_id,receiver_id,tp_location,flag,created_by,tmx_interchange_id) {

     var headers= new HttpHeaders();
     headers.append('Content-Type','application/X-www-form=urlencoded');

     return this.http.post(this.ediTradingPartnerUrl + '/add', {
       tp_name:tp_name,
       sender_id:sender_id,
       receiver_id: receiver_id,
       tp_location: tp_location,
       flag:flag,
       created_by:created_by,
       tmx_interchange_id:tmx_interchange_id
     }, {
       responseType: 'text'
     });

   }

  // Delete an EDI Trading Partner
  deleteEdiTradingPartner(edi_trading_partner_id: number) {

    return this.http.delete(this.ediTradingPartnerUrl + '/' + edi_trading_partner_id)
  }
}

我的API

<?php

    // Call database connection
    require_once('dbconnect.php');

// Display all records
    $app->get('/edi_trading_partner', function($req, $res) {

        $query = "SELECT *
                  FROM edi_trading_partner
                  order by id";
        $result = mysql_query($query);

        while($row = mysql_fetch_assoc($result)) {

            $data[] = $row;
        }

        if(isset($data)) {

            return $res->withJson($data);
            return json_encode($data, JSON_PRETTY_PRINT);
        }

        mysql_close($conn);
    });
    // Display single row
    $app->get('/edi_trading_partner/{id}', function($res) {

        $id = $res->getAttribute('id');


        $query = "SELECT *
                  FROM edi_trading_partner
                  WHERE id=$id";

        $result = mysql_query($query);

        while($row = mysql_fetch_assoc($result)) {

            $data[] = $row;
        }

        if(isset($data)) {

            return json_encode($data);
            return $res->withJson($data);
        } else {

            return json_encode($err_msg);
        }

        mysql_close($conn);

    });

    // Add new trading partner
    $app->post('/edi_trading_partner/add', function() {

        $adddata=json_decode(file_get_contents("php://input"));
        if(sizeof($adddata)!=0){

            $tp_name=$adddata->tp_name;
            $interchange_id=$adddata->interchange_id;
            $tmx_interchange_id=$adddata->tmx_interchange_id;
            $num = 0;
 $count = "SELECT * FROM edi_user order by id";
     $result1 = mysql_query($count);
        while($row = mysql_fetch_assoc($result1)) {
            $num++;
        }
        $num++;

            $query = "INSERT into edi_trading_partner (
                      sender_id,
                      receiver_id,
                      tp_name,
                      tp_location,
                      flag,
                      creation_date,
                      created_by,
                      tmx_interchange_id)
                      values (
                      '$sender_id', 
                      '$receiver_id', 
                      '$tp_name', 
                      '$tp_location', 
                      '$flag',
                      '$creation_date',
                      '$created_by', 
                      '$tmx_interchange_id')";

            $result = mysql_query($query) or die(json_encode(array('error' => 'error lang'), JSON_PRETTY_PRINT));

            echo json_encode(array('success' => 'added'));

            if(isset($result)) {
                header('Content-Type: application/json');
                echo json_encode($result);
            }

        mysql_close($conn);

        }
    });

    // Delete trading partner
    $app->map(['GET', 'DELETE'],'/edi_trading_partner/delete/{edi_trading_partner_id}', function($req, $res, $args) {

        $edi_trading_partner_id = $req->getAttribute('edi_trading_partner_id');

        $query = "DELETE from edi_trading_partner
                  WHERE edi_trading_partner_id='$edi_trading_partner_id'";

        $result = mysql_query($query);

        if(!$result) {

            return json_encode(
                array("message" => "Unable to delete this trading partner!")
            );
        } else {

            return json_encode(
                array("message" => "Trading partner successfully deleted!")
            );
        }
    });

我试图添加数据,但是无法添加到数据库。

0 个答案:

没有答案