Please help, bug while uploading a csv file [duplicate]
Possible Duplicate:
I have problem while uploading the csv file
The csv file will insert new values if the details for the particular month is not already present and it will update the rows with the new datas in the table if the details of the particular month is already present in the table by uploading the csv file.
The last row of the csv file is getting repeated all over the column in the table payslip. I know there is some mistake in my query or something. But i couldnt figure out what is wrong. can anyone please help me solve this issue ?
<?php
require_once '../config.php';
if(isset($_POST['upload']))
{
$fname = $_FILES['sel_file']['name'];
$month = $_POST['month'];
$chk_file = explode(".",$fname);
if(strtolower($chk_file[1]) == 'csv')
{
//$sel=mysql_query("select * from employee where month='$month'");
//$del=mysql_query("delete from employee where month='$month'");
$query1 = mysql_query("SELECT * FROM payslips where month='$month'");
$pay_num_rows = mysql_num_rows($query1);
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename,"r");
fgetcsv($handle,1000,",");
if($pay_num_rows > 1)
{
while(($data = fgetcsv($handle,1000,",")) != false)
{
$upd = "UPDATE payslips SET month='$data[9]',tot_work_days='$data[10]',lop_days='$data[11]',arrear_amt='$data[12]',leav e_encash='$data[13]' where month='$month'";
mysql_query($upd) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
if($pay_num_rows == 0)
{
while(($data = fgetcsv($handle,1000,",")) != false)
{
$sql = "INSERT into payslips(employee_code,employee_name,employee_address,开发者_运维知识库emp_dateofjoin,emp_designation,emp_hq ,pf_num,esic_num,emp_state,month,tot_work_days,lop_days,arrear_amt,leave_encash) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[ 7]','$data[8]','$month','$data[10]','$data[11]','$data[12]','$data[13]')";
//$upd = "UPDATE employee SET month='$data[9]',tot_work_days='$data[10]',lop_days='$data[11]',arrear_amt='$data[12]',leav e_encash='$data[13]' where month='$month'";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
}
}
?>
There are a lot of problems with the code you have posted, least of all the problem you are having. Firstly you are not escaping any variables, so SQL Injection is a trivial exercise.
I also notice that when you check to see if the month exists in the database your if
statement looks like this:
if($pay_num_rows > 1) ...
However this would indicate that you want to do an UPDATE
if there is more than one row, but your problem describes it will do an INSERT
if a particular month is not already present. I would start by fixing your conditional logic to be:
if($pay_num_rows >= 1){
// UPDATE QUERY HERE
}
else {
// INSERT QUERY HERE
}
The logic of updating if there is at least one row, or a particular month is not already present is now reflected in your structure.
Looking over your code again it appears your UPDATE loop will always update every record WHERE month='$month'
, but $month
is always the same for every loop, so when the query executes is matches every row it has already done. To be more clear, say your month is January and you have several rows in the database with the month equal to January it will update ALL those rows, therefore whatever the data in the last row is will be what is written to every row. You need to look into the WHERE
clause of that query.
精彩评论