meenahkur
meenahkur

Reputation: 33

update multiple author in php but not same value

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>";
}

Upvotes: 0

Views: 42

Answers (1)

Andre Rodrigues
Andre Rodrigues

Reputation: 362

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.

Upvotes: 1

Related Questions