I have 2 JNSERT queries…
I want 1 to run if a SELECT query returns 0 results using
//check if this power strip has a receptacle
try{
$sql = "SELECT receptacle_id FROM power_trails WHERE power_strip_id = ".$power_strip_id;
echo $sql;
$result = $pdo->query($sql);
if($result->rowCount() != 0){
while($row = $result->fetch()){
$receptacle_id = $row['receptacle_id'];
try{
$sql = "INSERT INTO power_trails (receptacle_id,power_strip_id,outlett_placement,".$key.") VALUES (".$receptacle_id.",".$power_strip_id.",".$Outlett.",".$asset_id.")";
$pdo->exec($sql);
header("location: add_power_strip_success.php?id=".$asset_id."&type=".$_POST['type']);
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
}
// Free result set
unset($result);
} else {
$sql = "INSERT INTO power_trails (power_strip_id,outlett_placement,".$key.") VALUES (".$power_strip_id.",".$Outlett.",".$asset_id.")";
$pdo->exec($sql);
header("location: add_power_strip_success.php?id=".$asset_id."&type=".$_POST['type']);
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
the query (when I run it in PHPMyAdmin seems to work, but returns nothing
Since no receptacle_id is found, why is this the result
Array
(
[power_strip_id] => 1
[Outlett] => 2
[type] => KVM
[asset_id] => 1
)
SELECT receptacle_id FROM power_trails WHERE power_strip_id = 1
ERROR: Could not able to execute INSERT INTO power_trails (receptacle_id,power_strip_id,outlett_placement,kvm_id) VALUES (,1,2,1). SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1,2,1)' at line 1
shouldnt the other INSERT query be run if the query returns nothing?