gpt4 book ai didi

php - INSERT 或重复 KEY UPDATE

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:26 24 4
gpt4 key购买 nike

我有一个填充的输入字段,其中包含来自名为 person 的 mysql 表中的数据。我能够将数据存储在表中或更新表中的数据。现在我正试图在一个查询中完成这两项工作; INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;。这些值共享一个名为 academy_id 的外键,因此为了更新每个值,我还必须指定名为 person_id 的唯一自动递增 ID。当我执行查询时出现错误 您的 SQL 语法有错误;查看与您的 MySQL 服务器版本相对应的手册,了解要使用的正确语法。我该如何解决这个问题并能够插入新值或更新当前值? EXAMPLE

SELECT 查询以显示表中的值

//SELECT Query to display values
$id = 15;
$db_select3 = $db_con->prepare("
SELECT a.name,
a.academy_id,
p.person_id,
p.first_name,
p.last_name
FROM academy a
LEFT JOIN person p ON a.academy_id = p.academy_id
WHERE a.academy_id = :id
");
if (!$db_select3) return false;
if (!$db_select3->execute(array(':id' => $id))) return false;
$results3 = $db_select3->fetchAll(\PDO::FETCH_ASSOC);
if (empty($results3)) return false;
$result3 = '';
echo "<strong>Personel Information:</strong>";
$s = 1;
foreach ($results3 as $value3){
echo "<ul id=\"pq_entry_".$s."\" class=\"clonedSection\">";
echo "Primary AI Key ID <b>person_id</b>: " . $value3['person_id'] . "</br>";
echo "Foreign Key ID <b>academy_id</b>: " . $value3['academy_id'] . "</br>";
echo "<li><input type=\"hidden\" name=\"person_id_".$s."\" value='". $person_id = $value3['person_id']."'/></li>";
echo "<li><input id=\"person_fname_".$s."\" name=\"person_fname_".$s."\" placeholder=\"Person #1 - First Name\" type=\"text\" value='" . $value3['first_name'] ."'/><input type=\"hidden\" name=\"person_id_".$s."\" value='". $person_id = $value2['person_id']."'/></li>";
echo "<li><input id=\"person_lname_".$s."\" name=\"person_lname_".$s."\" placeholder=\"Last Name\" type=\"text\" value='" . $value3['last_name'] ."'/></li>";
echo "</ul>";
$s++;
}
echo "<input type='button' id='btnAdd' value='add another Person' />
<input type='button' id='btnDel' value='Delete' /></br>";

插入/更新查询

if(isset($_POST['submit'])) {
//Insert or Update Values
$f = 1;
while(isset($_POST['person_fname_' . $f]))
{

$person_fname = $_POST['person_fname_' . $f];
$person_lname = $_POST['person_lname_' . $f];

$query_init3 = "INSERT INTO person (academy_id, first_name, last_name) VALUES (:id,:person_fname,:person_lname)
ON DUPLICATE KEY UPDATE person SET academy_id=:id, first_name=:person_fname, last_name=:person_lname WHERE academy_id=:id AND person_id=:person_id;";
$query_prep3 = $db_con->prepare($query_init3);
$query_prep3->execute(array(
"id" => $id,
"person_id" => $person_id,
"person_fname" => $person_fname,
"person_lname" => $person_lname
));

$f++;
}
}

表值:

+-----------+------------+-------------+-----------+
| person_id | academy_id | first_name | last_name |
+-----------+------------+-------------+-----------+
| 1 | 15 | James | Barkley |
| 2 | 15 | Cynthia | Smith |
| 3 | 8 | Peter | Black |
+-----------+------------+-------------+-----------+

最佳答案

您不需要指定 WHERE 条件,因为您已经在 UPDATE 上使用了子句。

尝试:

$query_init3 = "INSERT INTO person (academy_id, first_name, last_name) VALUES (:id,:person_fname,:person_lname) 
ON DUPLICATE KEY UPDATE academy_id=:id, first_name=:person_fname, last_name=:person_lname";

关于php - INSERT 或重复 KEY UPDATE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com