我已经阅读了几个SO线程,可以使用INNER JOIN
更新查询更新两个数据库,但我无法让它工作,它只是抛出错误:
致命错误:未捕获的异常' PDOException'消息' SQLSTATE [23000]:完整性约束违规:1052列'类别'在字段列表中是模糊的'在C:\ xampp \ htdocs \ update.php:79堆栈跟踪:#0 C:\ xampp \ htdocs \ update.php(79):PDOStatement->执行(数组)#1 {main}抛出C:\第79行的xampp \ htdocs \ update.php
第79行: $result = $stmt->execute($prepare);
if (isset($_POST['update'])) {
$category = isset($_POST['category']) ? $_POST['category'] : null;
$manufactuer = isset($_POST['manufactuer']) ? $_POST['manufactuer'] : null;
$model = isset($_POST['model']) ? $_POST['model'] : null;
$serial = isset($_POST['serial']) ? $_POST['serial'] : null;
$itemcondition = isset($_POST['itemcondition']) ? $_POST['itemcondition'] : null;
$locationb = isset($_POST['locationb']) ? $_POST['locationb'] : null;
$locationr = isset($_POST['locationr']) ? $_POST['locationr'] : null;
$comments = isset($_POST['comments']) ? $_POST['comments'] : null;
$purchased = isset($_POST['purchased']) ? $_POST['purchased'] : null;
$retired = isset($_POST['retired']) ? $_POST['retired'] : null;
$stolen = isset($_POST['stolen']) ? $_POST['stolen'] : null;
$sql_part = array();
$prepare = array();
if ($category){
$sql_part[] = 'category = :category';
$prepare[':category'] = $category;
}
if($manufactuer){
$sql_part[] = 'manufactuer = :manufactuer';
$prepare[':manufactuer'] = $manufactuer;
}
if($model){
$sql_part[] = 'model = :model';
$prepare[':model'] = $model;
}
if($serial){
$sql_part[] = 'serial = :serial';
$prepare[':serial'] = $serial;
}
if($itemcondition){
$sql_part[] = 'itemcondition = :itemcondition';
$prepare[':itemcondition'] = $itemcondition;
}
if($locationb){
$sql_part[] = 'locationb = :locationb';
$prepare[':locationb'] = $locationb;
}
if($locationr){
$sql_part[] = 'locationr = :locationr';
$prepare[':locationr'] = $locationr;
}
if($comments){
$sql_part[] = 'comments = :comments';
$prepare[':comments'] = $comments;
}
if($purchased){
$sql_part[] = 'purchased = :purchased';
$prepare[':purchased'] = $purchased;
}
if($retired){
$sql_part[] = 'retired = :retired';
$prepare[':retired'] = $retired;
}
if($stolen){
$sql_part[] = 'stolen = :stolen';
$prepare[':stolen'] = $stolen;
}
$prepare[':barcode'] = $barcode;
if(count($sql_part)){
$sql = 'UPDATE assets a INNER JOIN assets_history b ON (a.barcode = b.barcode) SET ';
$sql .= implode(', ', $sql_part);
$sql .= ' WHERE a.barcode = :barcode AND b.barcode = :barcode';
$stmt = $conn->prepare($sql);
if($stmt){
$result = $stmt->execute($prepare);
$count = $stmt->rowCount();
header('Location: ./usearch.php');
exit;
}
}
}
这是数据库结构,如果需要:
`barcode` int(6) UNSIGNED ZEROFILL NOT NULL
`category` text NOT NULL
`manufactuer` text NOT NULL
`model` varchar(255) NOT NULL
`serial` varchar(255) NOT NULL
`itemcondition` text NOT NULL
`locationb` text NOT NULL
`locationr` text NOT NULL,
`comments` varchar(255) NOT NULL
`purchased` varchar(30) NOT NULL
`retired` varchar(30) NOT NULL
`stolen` varchar(30) NOT NULL
是否有更好的方法可以做到这一点,或者我错过了一些愚蠢的事情。
我也刚刚看到了在PHPMyAdmin中关联两个表格列的可能性,但尚未尝试过。这两个表是相同的,只会保留对记录的所有更新。
我对这些文章没有运气,其中一篇是我的代码的基础。
MySQL UPDATE syntax with multiple tables using WHERE clause
MySql update two tables at once
How to update two tables in one statement in SQL Server 2005?
答案 0 :(得分:1)
使用table name
列category
列表
if ($category){
$sql_part[] = 'assets.category = :category';
$prepare[':category'] = $category;
}
答案 1 :(得分:1)
在我看来,资产' &安培; ' assets_history'两者都有'类别'领域。 因此,请更新您的代码:(用您的确切table_name替换' assets'或' assets_history'
if ($category){
$sql_part[] = '<TABLE_NAME>.category = :category';
$prepare[':category'] = $category;
}
答案 2 :(得分:1)
您必须附加表名或其别名,以避免像这样的混淆
if ($category){
$sql_part[] = 'a.category = :category';
$prepare[':category'] = $category;
}
if($manufactuer){
$sql_part[] = 'a.manufactuer = :manufactuer';
$prepare[':manufactuer'] = $manufactuer;
}
if($model){
$sql_part[] = 'a.model = :model';
$prepare[':model'] = $model;
}
if($serial){
$sql_part[] = 'a.serial = :serial';
$prepare[':serial'] = $serial;
}
if($itemcondition){
$sql_part[] = 'a.itemcondition = :itemcondition';
$prepare[':itemcondition'] = $itemcondition;
}
if($locationb){
$sql_part[] = 'a.locationb = :locationb';
$prepare[':locationb'] = $locationb;
}
if($locationr){
$sql_part[] = 'a.locationr = :locationr';
$prepare[':locationr'] = $locationr;
}
if($comments){
$sql_part[] = 'a.comments = :comments';
$prepare[':comments'] = $comments;
}
if($purchased){
$sql_part[] = 'a.purchased = :purchased';
$prepare[':purchased'] = $purchased;
}
if($retired){
$sql_part[] = 'a.retired = :retired';
$prepare[':retired'] = $retired;
}
if($stolen){
$sql_part[] = 'a.stolen = :stolen';
$prepare[':stolen'] = $stolen;
}
$prepare[':barcode'] = $barcode;
编辑: 您也可以这样做来更新所有列
$columns = array('category','manufactuer','model','serial','itemcondition','locationb','locationr','comments','purchased','retired','stolen');
$sql_part = array();
foreach($columns as $column){
$sql_part[] = 'a.'.$column.' = :'.$column;
$sql_part[] = 'b.'.$column.' = :'.$column;
$prepare[':'.$column] = '$'.$column;
}
这将为您节省几行代码