Hi all,
I have a code which works fine.

<?php
 
$db = mysql_connect("localhost","root",""); 
mysql_select_db("test",$db); 
 
$today = date("F j, Y, g:i a");
//echo $today;exit();
$file = "test.txt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);
 
$output = str_replace("\t|\t", "|", $data);
 
$output = explode("\n", $output);
 
foreach($output as $var) {
//$categories_id = $categories_id + 1;
$tmp = explode("|", $var);
$name= $tmp[0];
 
mysql_query("INSERT INTO user_details(name) VALUES('$name',) ") or die(mysql_error()); 
}
echo "Done!";
 
?>

This works fine. Now i want to add two fields (runtime_id and date).

Say for Ex: If i insert a file today the runtime_id should be 1 (any number of files can be used to insert into mysql but the runtime_id should be 1 for the same date) and all values should be inserted..
If i insert a file tomorrow the runtime_id should be 2
and the values from my text file should be inserted and so on...
Please help me.

Thanks
Haan

Before inserting into the table, fetch the latest date from the table like;
$res=mysql_query(

<?php
$res=mysql_query("select max(runtime_id) from user_details")
$row=mysql_fetch_array($res);
if($row)
{
    $runtime_id=$row['runtime_id'];
}
else
{
    $runtime_id=1;//insertion 1 for first time of execution
}
//now compare date to increase runtime_id 
$res=mysql_query("select max(date_field) from user_details")
$row=mysql_fetch_array($res);
if($row)
{
    if($today=$row['date_field'])
    {//$today is what already you are calculating in your code
        // if date is same do nothing,
    }
    else//else increment runtime_id by 1
    {
        $runtime_id=$runtime_id+1;//insertion 1 for first time of execution
    }
}
/* now use your insertion statement along with these fields
hope this will work
by-
Karthik A*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.