私はINNER JOIN
更新クエリを使用して2つのデータベースを更新することができるいくつかのSOスレッドで読みましたが、私はそれが仕事を得ることができません、それだけでエラーがスローされます。アップデート2つのMySQLデータベースの
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'category' in field list is ambiguous' in C:\xampp\htdocs\update.php:79 Stack trace: #0 C:\xampp\htdocs\update.php(79): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\update.php on line 79
LINE 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で2つのテーブルの列を関連付ける可能性を見ましたが、まだ試していません。 2つのテーブルは同じですが、すべての更新をレコードに保存します。
私はこれらの記事で運がなかった、そのうちの1つは私のコードの基礎です。
MySQL UPDATE syntax with multiple tables using WHERE clause
How to update two tables in one statement in SQL Server 2005?
MySQL, update multiple tables with one query
エラーは同じです –
update.phpの行番号79を確認してください$ SQLをエコーして目的をデバッグし、ここにSQLを投稿してください –
両方のテーブルに同じフィールドがあります。すべてのSQLパーツを 'assets.'に変更すると、問題は解決されますが、' assets'テーブルだけが更新され、 'assets_history'テーブルは更新されません。 –