I currently have this code connected to a JSGrid table:
$connect = new PDO("mysql:host=localhost;dbname=localDatabase", "root", "root");
$method = $_SERVER['REQUEST_METHOD'];
$query = "SELECT COUNT(*) FROM stmr";
$link = mysqli_connect("localhost","root", "root", "localDatabase");
$result = mysqli_query($link, $query);
$form_id = mysqli_fetch_array($result)[0] + 1;
if($method == 'GET')
{
$data = array(
':item_id' => "%" . $_GET['item_id'] . "%",
':description' => "%" . $_GET['description'] . "%",
':part_number' => "%" . $_GET['part_number'] . "%",
':unit' => "%" . $_GET['unit'] . "%",
':quantity' => "%" . $_GET['quantity'] . "%"
);
$query = "SELECT * FROM stmrdesc WHERE item_id LIKE :item_id AND description LIKE :description AND part_number LIKE :part_number AND unit LIKE :unit AND quantity LIKE :quantity ORDER BY item_id DESC";
$statement = $connect->prepare($query);
$statement->execute($data);
$result = $statement->fetchAll();
foreach($result as $row)
{
$output[] = array(
'id' => $row['row_id'],
'item_id' => $row['item_id'],
'description' => $row['description'],
'part_number' => $row['part_number'],
'unit' => $row['unit'],
'quantity' => $row['quantity']
);
}
header("Content-Type: application/json");
echo json_encode($output);
}
if($method == "POST")
{
$data = array(
':item_id' => $_POST['item_id'],
':description' => $_POST['description'],
':part_number' => $_POST["part_number"],
':unit' => $_POST["unit"],
':quantity' => $_POST["quantity"]
);
$query = "INSERT INTO stmrdesc (item_id, description, part_number, unit, quantity) VALUES (:item_id, :description, :part_number, :unit, :quantity)";
$statement = $connect->prepare($query);
$statement->execute($data);
}
if($method == 'PUT')
{
parse_str(file_get_contents("php://input"), $_PUT);
$data = array(
':item_id' => $_PUT['item_id'],
':description' => $_PUT['description'],
':part_number' => $_PUT['part_number'],
':unit' => $_PUT['unit'],
':quantity' => $_PUT['quantity']
);
$query = "
UPDATE 'stmrdesc'
SET 'item_id' = ':item_id',
'description' = ':description',
'part_number' = ':part_number',
'unit' = ':unit',
'quantity' = ':quantity'
WHERE 'id' = ':id'
";
$statement = $connect->prepare($query);
$statement->execute($data);
}
if($method == "DELETE")
{
parse_str(file_get_contents("php://input"), $_DELETE);
$query = "DELETE FROM stmrdesc WHERE id = '".$_DELETE["id"]."'";
$statement = $connect->prepare($query);
$statement->execute();
}
As you can see, I have a method that is connected to each of the buttons of the JSGrid table and all work (submit the changes to the database) except the PUT Method that edits and updates the row in the database.
The put sql statement has WHERE 'id' = ':id'
but ':id' does not exist in the $data array.