0

php isn't passing $id to query

I am trying to execute an inline edit script using data pulled from a mysqli query that pulls specific data based on url ?id= using if isset $_GET $id, the page is getting and echoing the id correctly, however, the query isn't getting the $id variable.

I have tested the query by replacing the $id var with a number relative to the data and it works without issue.

I have tried adding the $id into the $_SESSION and retrieving it from there but still no luck.

The main page is an index.php (which has url of index.php?id=2019018) which fetches data and displays it as a datagrid with inline edit capability through js (fetch_data.php).

you may notice tests etc that have been commented out

both scripts are below, any help appreciated

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
  • 1
    The call to fetch_data is a separate request, if the ID is in the query string of the main page it isn't automatically passed to the Ajax request, you'll have to provide it. I see you send some data in the request, but it's not clear what the contents will be, from the code provided Commented Apr 1, 2019 at 9:27

1 Answer 1

1

You need to pass the id to your AJAX request too since it is considered a totally separate request.

e.g.

insertItem: function (item) {
            return $.ajax({
                type: "POST",
                url: "fetch_data.php?id="<?php echo $id; ?>,
                data: item
            });
        },
Sign up to request clarification or add additional context in comments.

2 Comments

Just tried this and it doesnt work, it stop the datagrid from functioning.. unsure why, doing some error checking now.
@SeanHaymer Make sure $id is set and not undefined. Assign id like $id = isset($_GET['id']) ? $_GET['id'] : ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.