dongyin2390 2017-01-24 16:20
浏览 8
已采纳

在PHP中更新多个作者但不是相同的值

I want to update multiple authors but when i click the update button it saves last inputted value on all of the fields. Your response is highly appreciated. Thank you so much!!!

Here is my code

 <?php 
                        }elseif($ID[0]=="Update"){ 
                            $Author_Query = mysql_query("SELECT a.*, b.* FROM tblAuthor a, tblResources b WHERE a.Accession_No = b.Accession_No AND a.Accession_No = '".$ID[1]."'");
                            while($Author = mysql_fetch_array($Author_Query)){
                    ?>
                    <td><input type="text" name="Author[]" value="<?php echo $_POST['Author']; ?><?php echo $Author['Author']; ?>" /></td>
                    <?php }} ?>

Update button code if($_POST['Update'] == "Update"){

    $Ac1 = $_POST['Accession1'];
    $Ac2 = $_POST['Accession2'];



    do{

    mysql_query("UPDATE tblResources SET

    Accession_No    = '".$Ac1."',
    Month           = '".$_POST['Month']."',
    Day             = '".$_POST['Day']."',
    RYear           = '".$_POST['RYear']."',
    Class           = '".$_POST['Class']."',
    Title           = '".$_POST['Title']."',
    Edition         = '".$_POST['Edition']."',
    Volumes         = '".$_POST['Volumes']."',
    Pages           = '".$_POST['Pages']."',
    Source_of_Fund  = '".$_POST['Source_of_Fund']."',
    Cost_Price      = '".$_POST['Cost_Price']."',
    Publisher       = '".$_POST['Publisher']."',
    Year            = '".$_POST['Year']."',
    Remarks         = '".$_POST['Remarks']."',
    Category_ID     = '".$_POST['Category']."',
    Type_ID         = '".$_POST['ResType']."',
    Copies          = '1',
    Availability    = '".$_POST['Availability']."',
    Tag             = 'Title',
    Year_Level      = '".$_POST['Year_Level']."'
    WHERE 
    Accession_No    = '".$Ac1."'");


     //$q = $Text."/";
     //$gets = explode("/",$q);

    $Get = $_POST['Author'];
    $Box = count($Get);
    for($Text = 0; $Text < $Box; $Text++){
    $TextBox = $Get[$Text];

    mysql_query("UPDATE tblAuthor 
    SET Author = '".$TextBox."'
    WHERE Accession_No  = '".$Ac1."'");


    }

    $Ac1++;
    }while($Ac1 <= $Ac2);


    echo "<script language=javascript>
    alert('Successfully Updated!');
    location.href='Resources.php';
    </script>";
}
  • 写回答

1条回答 默认 最新

  • dongrao9454 2017-01-24 16:53
    关注

    The problem happen because field 'Accession_No' it's same for author, so the effective update always the last one.

    I think something like this solve your problem:

    $Get = $_POST['Author'];
    $Box = count($Get);
    
    if($Box > 0) {
        mysql_query("DELETE FROM tblAuthor  WHERE Accession_No  = '".$Ac1."'");
    
        foreach($Get as $author){
            mysql_query("INSERT INTO tblAuthor (Author, Accession_No) VALUES ('".$author."', ".$Ac1.");
        }
    }
    

    It's necessary remove the author, because if one author isn't in table before, the mysql not update, the record doesn't exists. So, it's easy remove all authors and insert again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟