INSERT或重复KEY UPDATE

时间:2013-12-02 22:49:40

标签: php mysql

我有一个填充的输入字段,其中包含来自mysql表的数据person。我能够在表中存储数据或更新表中的数据。现在我试图在一个查询中完成两个任务; INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;。这些值共享一个名为academy_id的外键,因此为了更新每个值,我还必须指定名为person_id的唯一自动增量ID。当我执行查询时,我收到错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use。如何解决这个问题并能够插入新值或更新当前值? EXAMPLE

SELECT Query以显示表

中的值
//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>";

INSERT / UPDATE查询

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     |  
+-----------+------------+-------------+-----------+

1 个答案:

答案 0 :(得分:3)

您不需要指定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";