Updating database

Discussion in 'PHP' started by sunilmadhav, Jan 12, 2010.

  1. #1
    Hi all,

    I have a datebase and i want to update it. I have a text file and i have to update the database using the text file. If there is same record in the text file and the database it has to over write if the record is not there in the database but there in the text file it has to add in the database. Can any one suggest me how to do that as iam new to PHP. I am pasting the code here

    <?php

    session_start();
    include("../connect2db.php");

    $user_descrip = $_SESSION['user_descrip'];
    $user_descrip="Administrator";
    if($user_descrip!="Administrator")
    {
    header("Location: ../accessdenied.htm");
    }
    else
    {
    $table="nielupdate";

    //AF
    $sql="update $table set custdept='AF' where substring(BIC1,1,1)='F'";
    mysql_query($sql);

    //JF
    $sql="update $table set custdept='JF' where substring(BIC1,1,1)='Y' and substring(BIC1,2,1)='F'";
    mysql_query($sql);

    //JNF
    $sql="update $table set custdept='JNF' where substring(BIC1,1,1)='Y' and substring(BIC1,2,1)!='F'";
    mysql_query($sql);

    //ANF
    $sql="update $table set custdept='ANF' where custdept='' and substring(BIC1,1,1)!='Y' and substring(BIC1,1,1)!='F' and BIC1!=''";
    mysql_query($sql);

    //Binding Paperback
    $sql="update $table set binding='Paperback' where binding='' or binding= 'NULL' and substring(FMC,1,2)='BC'";
    mysql_query($sql);

    //Binding Hardback
    $sql="update $table set binding='Hardback' where binding='' or binding= 'NULL' and substring(FMC,1,2)='BB'";
    mysql_query($sql);

    //JF
    $sql="update $table set custdept='JF' where custdept='' and substring(dw,1,1)='8' and substring(dw,3,1)='3' and (substring(rc,1,1)='S' or substring(rc,1,1)='J' or substring(rc,1,1)='X')";
    mysql_query($sql);


    //AF
    $sql="update $table set custdept='AF' where custdept='' and substring(dw,1,1)='8' and substring(dw,3,1)='3' and (substring(rc,1,1)!='S' or substring(rc,1,1)!='J' or substring(rc,1,1)!='X')";
    mysql_query($sql);

    //JNF
    $sql="update $table set custdept='JNF' where custdept='' and substring(dw,1,1)!='8' and substring(dw,3,1)!='3' and dw!='' and (substring(rc,1,1)='S' or substring(rc,1,1)='J' or substring(rc,1,1)='X')";
    mysql_query($sql);

    //ANF
    $sql="update $table set custdept='ANF' where custdept='' and substring(dw,1,1)!='8' and substring(dw,3,1)!='3' and dw!='' and (substring(rc,1,1)!='S' or substring(rc,1,1)!='J' or substring(rc,1,1)!='X')";
    mysql_query($sql);


    //fiction Y
    $sql="update $table set fiction='Y' where custdept='AF' or custdept='JF'";
    mysql_query($sql);

    //fiction N
    $sql="update $table set fiction='N' where custdept!='AF' and custdept!='JF'";
    mysql_query($sql);

    //copy date
    $sql="update $table set pub_date=PDUK";
    mysql_query($sql);

    //fix type HW281208
    $sql="update $table set type1='BOOKS' where type1=''";
    mysql_query($sql);

    //Update type1 and binding to BOOKS
    $sql0="select catalog_no,fmc from $table";
    $res0=mysql_query($sql0);
    while($ser0=mysql_fetch_array($res0))
    {
    $fmc=$ser0['fmc'];
    $catalog_no=$ser0['catalog_no'];
    $num_rows = mysql_num_rows(mysql_query("select type1 from neilsen_codes where fmc='$fmc'"));
    If ($num_rows > 0)
    {
    $type1=mysql_result(mysql_query("select type1 from neilsen_codes where fmc='$fmc'"),0);
    }
    $num_rows = mysql_num_rows(mysql_query("select binding from neilsen_codes where fmc='$fmc'"));
    If ($num_rows > 0)
    {
    $binding=mysql_result(mysql_query("select binding from neilsen_codes where fmc='$fmc'"),0);
    }
    mysql_query("update $table set type1='$type1' where catalog_no='$catalog_no'");
    mysql_query("update $table set binding='$binding' where catalog_no='$catalog_no'");
    }
    }
    ?>


    Thanks,
    Sunil
     
    sunilmadhav, Jan 12, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should take a look at the INSERT IGNORE of mysql
     
    JAY6390, Jan 12, 2010 IP
  3. sunilmadhav

    sunilmadhav Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi jay6390,

    $sql="load data infile 'upd_20090527c.txt' into table $table";
    $sql="insert ignore into $table select * from 'upd_20090527c.txt'";
    mysql_query($sql);

    i tried both but none of them is working
     
    sunilmadhav, Jan 12, 2010 IP