prob in updating the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sunilmadhav
Forum Newbie
Posts: 1
Joined: Tue Jan 12, 2010 5:16 am

prob in updating the database

Post by sunilmadhav »

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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: prob in updating the database

Post by social_experiment »

In the code example below, is 'substring(BIC1, 1,1)' supposed to be a field name?

Code: Select all

 
<?php
 $sql="update $table set custdept='AF' where substring(BIC1,1,1)='F'";
 mysql_query($sql);
?>
 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply