我有一个PHP脚本,可以用参数调用。如果在不带参数的情况下调用,它将显示数据库中的一些数据。如果用参数" add“调用,它会将一些数据添加到db中,然后它应该调用与上面相同的查询并显示来自db的数据-但它没有!
未安装任何连接,未使用任何事务。
如果使用了参数"delete“,它的工作方式应该是正确的。
同样奇怪的是,值被正确地插入到db中。如果我在没有参数"action“的情况下第二次运行脚本,则会显示先前插入的值。Insert-query似乎工作正常。
对我来说,似乎有一个事务没有完全完成,但在数据行/表仍然被阻塞的情况下,对新数据集的查询已经在运行。
有什么想法吗?
if (isset($_GET['action'])) {
if($_GET['action'] == 'add'){
if (isset($_GET['propid'])) {
$query = "SELECT id as property_id, name, enumtype, openentries, required FROM properties WHERE id = :prop_id";
$params = array();
$params[] = array(':prop_id', $_GET['propid'], 'int');
$db->query($query, $params);
$property = $db->fetch();
// Assign property to categeory
$query = "INSERT INTO categories_properties(cat_id, property_id, required) VALUES (:cat_id, :prop_id, :req)";
$params = array();
$params[] = array(':cat_id', $catid, 'int');
$params[] = array(':prop_id', $_GET['propid'], 'int');
$params[] = array(':req', $property['required'], 'int');
$db->query($query, $params);
$success = $db->result();
}
}elseif($_GET['action'] == 'delete'){
if (isset($_GET['propid'])) {
// Assign property to categeory
$query = "DELETE FROM categories_properties WHERE cat_id = :cat_id AND property_id = :prop_id";
$params = array();
$params[] = array(':cat_id', $catid, 'int');
$params[] = array(':prop_id', $_GET['propid'], 'int');
$db->query($query, $params);
}
}
}
// Get assigned properties
$query = "SELECT cp.cat_id cat_id, cp.property_id property_id, cp.required required, p.name property_name, p.enumtype property_enum, c.cat_name cat_name FROM (categories_properties cp INNER JOIN properties p ON (cp.property_id = p.id)) LEFT OUTER JOIN categories c ON (cp.cat_id = c.cat_id) WHERE cp.cat_id = :cat_id";
$params = array();
$params[] = array(':cat_id', $catid, 'int');
$db->query($query, $params);
$cat_properties = array();
while ($row = $db->fetch()) {
$cat_properties[] = $row;
}
print_r($cat_properties); // <<< empty if action == add, correct if action == delete or action not set
发布于 2019-06-05 16:39:49
这段代码取自一个开源项目,但其中有很多非结构化代码(如上图所示)。我现在换成了我自己的旧PDO类。它工作得很好,没有任何问题,但现在我不得不修改这个项目中的许多代码,但我认为最后它是值得的。
https://stackoverflow.com/questions/56443007
复制相似问题