查询不会从页面

时间:2019-04-01 09:20:26

标签: javascript php mysql

php没有通过$id进行查询

我正在尝试使用从mysqli查询中提取的数据执行内联编辑脚本,该查询使用if isset ?id= $_GET根据URL $id提取特定数据,页面正在正确获取并回显id,但是查询未获取$id变量。

我已经通过将$id var替换为相对于数据的数字来测试查询,并且该查询没有问题。

我尝试将$id添加到$_SESSION并从那里检索,但还是没有运气。

主页是index.php(其网址为index.php?id=2019018),该页面可获取数据并将其显示为具有通过js(fetch_data.php)进行内联编辑功能的数据网格。

您可能会注意到已被注释掉的测试等

这两个脚本均在下方,感谢您的帮助

index.php

<html>  
    <head>  
        <title>Inline Table Insert Update Delete in PHP using jsGrid</title>  
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
        <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
        <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
        <style>
            .hide
            {
                display:none;
            }
        </style>
    </head>  
    <body>  
        <div class="container">  
            <br />
            <div class="table-responsive">  
                <h3 align="center">Inline Table Insert Update Delete in PHP using jsGrid</h3><br />
                <div id="grid_table"></div>
            </div>  
        </div>
        <?php
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
        }
        //session_start();
        //$_SESSION['id_sess'] = $id;
        ?>
        <?php
        // echo $_SESSION['id_sess'];
        echo $id;
        ?>

    </body>  
</html>  
<script>

    $('#grid_table').jsGrid({
        width: "100%",
        height: "600px",
        filtering: true,
        inserting: true,
        editing: true,
        sorting: true,
        paging: true,
        autoload: true,
        pageSize: 10,
        pageButtonCount: 5,
        deleteConfirm: "Do you really want to delete data?",
        controller: {
            loadData: function (filter) {
                return $.ajax({
                    type: "GET",
                    url: "fetch_data.php",
                    data: filter
                });
            },
            insertItem: function (item) {
                return $.ajax({
                    type: "POST",
                    url: "fetch_data.php",
                    data: item
                });
            },
            updateItem: function (item) {
                return $.ajax({
                    type: "PUT",
                    url: "fetch_data.php",
                    data: item
                });
            },
            deleteItem: function (item) {
                return $.ajax({
                    type: "DELETE",
                    url: "fetch_data.php",
                    data: item
                });
            },
        },
        fields: [
            {
                name: "job_id",
                type: "text",
                //css: 'hide'
            },
            {
                name: "part_id",
                type: "text",
//css: 'hide'
            },
            {
                name: "part_name",
                type: "text",
                width: 150,
                validate: "required"
            },
            {
                name: "part_cost",
                type: "text",
                width: 150,
                validate: "required"
            },
            {
                name: "part_rrp",
                type: "text",
                width: 50,
                validate: "required"
            },
            {
                name: "quantity",
                type: "text",
                width: 50,
                validate: "required"
            },
            {
                type: "control"
            }
        ]

    });

</script>

fetch_data.php

<?php
//$id = $_GET['id'];
//$id = $_SESSION['id_sess'];

$connect = new PDO("mysql:host=localhost;dbname=****", "****", "****");

$method = $_SERVER['REQUEST_METHOD'];

/* if(!isset($_GET['id'])) // if it doesnt get id?
  {
  echo "IT WORKS";
  //$id = $_GET['id'];
  }else{
  $id = $_GET['id'];
  } */
if ($method == 'GET') {
    $data = array(
        ':part_name' => "%" . $_GET['part_name'] . "%",
        ':part_cost' => "%" . $_GET['part_cost'] . "%",
        ':part_rrp' => "%" . $_GET['part_rrp'] . "%",
        ':quantity' => "%" . $_GET['quantity'] . "%"
    );

    //$query = "SELECT job_id, part_id, part_name, part_cost, part_rrp, quantity FROM jobs INNER JOIN job_parts USING (job_id) INNER JOIN parts USING (part_id) Where job_id = 2019018";

    $query = "SELECT job_id, part_id, part_name, part_cost, part_rrp, quantity FROM jobs INNER JOIN job_parts USING (job_id) INNER JOIN parts USING (part_id) Where job_id = '$job_id'";

    $statement = $connect->prepare($query);
    $statement->execute($data);
    $result = $statement->fetchAll();
    foreach ($result as $row) {
        $output[] = array(
            'part_id' => $row['part_id'],
            'part_name' => $row['part_name'],
            'part_cost' => $row['part_cost'],
            'part_rrp' => $row['part_rrp'],
            'quantity' => $row['quantity']
        );
    }
    header("Content-Type: application/json");
    echo json_encode($output);
}

if ($method == "POST") {
    $data = array(
        ':part_name' => $_POST['part_name'],
        ':part_cost' => $_POST["part_cost"],
        ':part_rrp' => $_POST["part_rrp"]
    );

    $query = "INSERT INTO parts (part_name, part_cost, part_rrp) VALUES (:part_name, :part_cost, :part_rrp)";
    $statement = $connect->prepare($query);
    $statement->execute($data);
}

if ($method == 'PUT') {
    parse_str(file_get_contents("php://input"), $_PUT);
    $data = array(
        ':part_id' => $_PUT['part_id'],
        ':part_name' => $_PUT['part_name'],
        ':part_cost' => $_PUT['part_cost'],
        ':part_rrp' => $_PUT['part_rrp']
    );
    $query = "
     UPDATE parts 
     SET part_name = :part_name, 
     part_cost = :part_cost, 
     part_rrp = :part_rrp
     WHERE part_id = :part_id
     ";
    $statement = $connect->prepare($query);
    $statement->execute($data);
}

if ($method == "DELETE") {
    parse_str(file_get_contents("php://input"), $_DELETE);
    $query = "DELETE FROM parts WHERE part_id = '" . $_DELETE["part_id"] . "'";
    $statement = $connect->prepare($query);
    $statement->execute();
}
?>

1 个答案:

答案 0 :(得分:1)

您还需要将@Injectable() export class FormService { // get all values of the formGroup, loop over them // then mark each field as touched public markFormGroupTouched(formGroup: FormGroup) { Object.values(formGroup.controls).forEach(control => { control.markAsTouched(); if (control.controls) { control.controls.forEach(c => this.markFormGroupTouched(c)); } }); } 传递给AJAX请求,因为它被认为是完全独立的请求。

例如

public buildForm() {
      this.registerForm = this.form.group({
        firstname: ['', [Validators.required, Validators.minLength(4), CustomValidators.validateCharacters]],
        lastname: ['', [Validators.required, CustomValidators.validateCharacters]],
        email: ['', [Validators.required, Validators.email]],
        password: ['', [Validators.required]],
      });