dongna1593 2013-12-03 01:41
浏览 13
已采纳

查询删除然后重新插入值

I am have data from mysql table person populate several input fields through a foreach loop. All values share a foreign key called academy_id. I am able to run a single query to store values or run a query to update the values. I am now trying to accomplish the ability to do Delete the values stored in the table and then re insert the values. When trying to do so I get a syntax error near the Insert query statement. How can I delete the value first and then re insert it in the table? Here is an live EXAMPLE

//SELECT Query to display values

<?php
$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;
echo "<strong>Personel Information:</strong>";
$s = 1;
foreach ($results3 as $value3){ 
    echo "<ul id=\"pq_entry_".$s."\" class=\"clonedSection\">";
    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>";

Delete then Insert values - Problem here

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 = "DELETE FROM person WHERE academy_id=:id  INSERT INTO person (academy_id, first_name, last_name) VALUES (:id,:person_fname,:person_lname);";
            $query_prep3 = $db_con->prepare($query_init3);
            $query_prep3->execute(array(
                "id" => $id,
                "person_fname" => $person_fname,
                "person_lname" => $person_lname
            ));

            $f++;
        }
}
  • 写回答

1条回答 默认 最新

  • dongque6377 2013-12-03 03:00
    关注

    Try doing two separate queries to achieve this for example run your Delete query first:

    $query_init3 = "DELETE FROM person WHERE academy_id=:id;";
    $query_prep3 = $db_con->prepare($query_init3);
    $query_prep3->execute(array(
    "id" => $id
    ));
    

    Then run your Insert query:

      $query_init4 = "INSERT INTO person (academy_id, first_name, last_name) VALUES (:id,:person_fname,:person_lname);";
                $query_prep4 = $db_con->prepare($query_init4);
                $query_prep4->execute(array(
                    "id" => $id,
                    "person_fname" => $person_fname,
                    "person_lname" => $person_lname
                ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法