PHP PDO更新有2个表(PHP PDO Update with 2 tables)
我有一点问题。 这就是我想要的:我有2个mysql表(类别,通道),通道表中有一个cat_id。 我想更新/编辑产品并将其放在另一个类别中,但即使产品的父ID(cat_id)为5,我创建的代码也只显示一个类别(id = 1)。
try {
//prepare query
$query = "select channel_id, name, category_id from channels where channel_id = ? limit 0,1";
$stmt = $pdo->prepare( $query );
//this is the first question mark
$stmt->bindParam(1, $_REQUEST['id']);
//execute our query
$stmt->execute();
//store retrieved row to a variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//values to fill up our form
$channel_id = $row['channel_id'];
$name = $row['name'];
$category_id = $row['category_id'];
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
$query2 = "SELECT * FROM categories";
$stmt2 = $pdo->prepare( $query2 );
$stmt2->execute();
$results = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
Channel Name | |
Category | |
I have a little problem. This is what I want to achive: I have 2 mysql tables (categories, channels), the channel table has a cat_id in it. I want to update/edit a product and place it in another category but the code that I've made shows just one category (id=1) even if the product has a parent id(cat_id) of 5.
try {
//prepare query
$query = "select channel_id, name, category_id from channels where channel_id = ? limit 0,1";
$stmt = $pdo->prepare( $query );
//this is the first question mark
$stmt->bindParam(1, $_REQUEST['id']);
//execute our query
$stmt->execute();
//store retrieved row to a variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//values to fill up our form
$channel_id = $row['channel_id'];
$name = $row['name'];
$category_id = $row['category_id'];
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
$query2 = "SELECT * FROM categories";
$stmt2 = $pdo->prepare( $query2 );
$stmt2->execute();
$results = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
Channel Name | |
Category | |
原文:https://stackoverflow.com/questions/19861467
更新时间:2020-01-05 06:59
最满意答案
代替
尝试:
Instead of
Try:
2013-11-08
相关问答
实际上, PDO::beginTransaction()遵循与MySQL中的START TRANSACTION语句相同的规则, 这里有更多信息。 这意味着MySQL不会立即锁定表,但会遵循ACID规则。 请注意,如果您需要显式锁定某些表,您可以执行以下操作: $db->beginTransaction();
$db->exec('LOCK TABLES t1, t2, ...');
Actually PDO::beginTransaction() follows the same rules a
...
代替 <?php foreach($results as $rows) {?>