I'm having trouble making WHERE IN query work, I was only able to make it work as a foreach loop.
My payload looks like this:
sku[]: BOOK0002
sku[]: BOOK0003
My API file code is as follows:
public function deleteProduct($prdt_sku)
{
try {
$this->db->query("DELETE FROM products WHERE product_sku IN :sku");
$this->db->bind(":sku", $prdt_sku);
if ($this->db->execute()) {
return true;
} else {
return false;
}
} catch (Throwable $e) {
echo 'Error deleting product from database. Reirecting...';
header("HTTP/1.1 406 Error deleting product from database");
header("refresh:3;url=https://metwesh.github.io/scandiweb-product-page/add-product");
}
}
My actual form destination:
$response = array();
if (isset($_POST['sku'])) {
foreach ($sku as $deletables) {
$result = $api->deleteProduct($deletables);
}
if ($result) {
header("HTTP/1.1 200 OK");
header("Location: https://metwesh.github.io/scandiweb-product-page/add-product");
exit();
} else {
header("HTTP/1.1 406 Error deleting product");
}
} else {
header("HTTP/1.1 499 Required parameters missing");
echo 'Please select one or more of the checkboxes. Reirecting...';
header("refresh:3;url=https://metwesh.github.io/scandiweb-product-page/");
}
I need to use the WHERE IN clause so I can have only one query per form & reduce the unnecessary amount of queries I'm using right now. Any help would be greatly appreciated, thanks.
Update My PDO:
<?php
class Database
{
private $host = DB_SERVER;
private $user = DB_USERNAME;
private $pass = DB_PASSWORD;
private $dbname = DB_NAME;
private $dbh;
private $stmt;
private $error;
public function __construct()
{
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
echo $this->error;
}
}
public function query($sql)
{
$this->stmt = $this->dbh->prepare($sql);
}
public function bind($param, $value, $type = null)
{
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute()
{
return $this->stmt->execute();
}
public function resultSet()
{
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
public function single()
{
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
}
```
$this->db->query("DELETE FROM products WHERE product_sku IN :sku");
? Also, in the second snippet:$sku
is not declared so I would expectforeach ($sku as $deletables) {
to throw an exception... \$\endgroup\$foreach ($sku as $deletables) { $result = $api->deleteProduct($deletables); }
keeps overwriting the boolean value, yourif ($result)
will only be respecting the last iteration's value. Do you want to be more accurately checking if the DELETE query affected any rows? Do you need anexit()
in yourcatch()
block? Why are thereheader()
s afterecho
s in your scripts? Does this code work as intended??? \$\endgroup\$IN
. Binding only generates a string;IN
does not want a string. \$\endgroup\$