View Full Version : insert problem

02-21-2007, 04:55 AM
hi, i have a page which is to show all the patient records(tblpatient)...then i need to check each treatment date izzit more than 6 months....if yes, it will insert into another table(tbloverdue).then, i have a condition to avoid same record insert twice whenever user browse the page...

but now, the problem is the first record in tblpatient can only insert once to tbloverdue...but then the second record and the rest will insert whenever user browse that page...(let say all records in tblpatient,the treatment date is more than 6 months)

$query = "SELECT *, (YEAR(CURDATE())-YEAR(DOB)) - (RIGHT(CURDATE(),5)<RIGHT(DOB,5)) AS Age FROM tblpatient";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)){
$TD = $row["Treatment_Date"];
<td> <center> <input type="checkbox" name="choice" value="<?php echo $row["Patient_ID"];?>">Yes </center> </td>
<td><a href="javascript:ccf_view('<?php echo $row["Patient_ID"];?>')"><?php echo $row["Patient_ID"];?></a></td>
<td><?php $yy = substr($row["Treatment_Date"],0,4);
$mm = substr($row["Treatment_Date"],5,2);
$dd = substr($row["Treatment_Date"],8,2);
$date1= $dd.'/'.$mm.'/'.$yy;
echo $date1;?></td>
<td><?php echo $row["Name"];?></td>
<td><?php if (($row["Email"]) == "")
echo 'NO';
echo $row["Email"];?></td>

$diff = datediff("$TD","now","m");
if (($row["Email"]) == "")
$row["Email"] = "NO";

$query2 = "Select * from tbloverdue";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_assoc($result2);
//while ($row2 = mysql_fetch_assoc($result2)){

if ($diff > 5){
if($row2["Patient_ID"] != $row["Patient_ID"]){

$query1 = "Insert into tbloverdue(Patient_ID, Name, Treatment_Date, Email)values('".$row["Patient_ID"]."', '".$row["Name"]."', '".$row["Treatment_Date"]."', '".$row["Email"]."')";
$result1 = mysql_query($query1);


can anyone help me pls?????

02-21-2007, 06:17 AM
your PHP is unusually placed for its job (in the middle of all the HTML)
im not saying this is wrong just unusual.

I would place all your queries above the <HTML> tags and only echo results within the HTML

also you want something else to happen if a condition is met

Have you looked into triggers within Mysql (version 5+)?

i need to rewrite this in my own style. This may take a day or so

i need your database creation script ddl so i can simulate your model

02-21-2007, 02:05 PM
oh...thank u very much...for triggers,i'm not so understand...i try to learn it from net...but not really understand....

im using Wampserver....so i dunno where to find the dll....im so sorry....any other way to simulate it???

and 1 more thing, datediff actually is a function i download from internet....do u need it???if yes, izzit jz paste it all here????

sorry for any inconvenient....

02-21-2007, 02:45 PM
i will write a function for date diff

your ddl is the script from the database which will allow me to create your tables and set up your environment.

do you understand PHP or are you just copying scripts of the net

02-22-2007, 01:17 AM

i try to find the dll file, but still could not find it...u know wampserver???it has an interface for u to create tables and all those thing...it is diff from the normal mysql...i think it is an open source..is it correct???if i write out the data dic???is it ok???im so sorry...

actually im not so good in php...some basic concept or logic i still can understand...like the datediff function...if u ask me to write...sure impossible...coz my programming skill = F,kekekeke...so most of the time i find from the internet and make some modification...

luckily u r trying to help me....or else...me = DIE

thank u so much

02-22-2007, 06:20 AM
i think the best thing would be to get you to start this again

please have a look at this...


you need to understand this properly because your going to have allot of difficulty later on

02-22-2007, 08:22 AM
okok...i sure will have a look....thank u so much

if i export my database...izzit ok??

02-22-2007, 08:27 AM
follow the tuts then take what you learn and adapt that to suit your requirements

dont try to interpret the tuts into your requirements.
Learn the concept first

02-22-2007, 09:44 AM
i have go through the tut...i found that my code has a lot of diff...i should learn it early...sigh!!!

but i need to pass up this system next week...so many things need to enhance....izzit ok i use my own style???

the judge is jz will look at my system but not my code....

02-22-2007, 09:48 AM
you can use any style you want

follow the tuts and modify the examples until you get what you want then start with the pretty stuff

02-22-2007, 10:14 AM
yeah...but now the problem is that i posted above...i try plenty of times...but the result is still the same.....can u pls help me to solve that problem first???


02-22-2007, 10:18 AM
im sorry

that would mean rewriting the script which you dont understand anyway

i would be doing this for you which i never do unless there is involved coz i need to eat and drink and pay for a bmw z4

i am willing to give up my time as long as the student gives up their time to learn.

its a 2 way street.

02-22-2007, 10:54 AM
okok...i understood...i will try to do my best first....anything will post a question here again....

anyway...thanks for wasting ur valuable time to help me...:grin:

02-22-2007, 10:59 AM
i never waste my time on others

its like driving a car..

I will show you what the pedals do and where the indicator stalk is and where the lights are. I will even use my own precious time to show you some engine maintenance but i will never drive you !

02-22-2007, 11:22 AM
then can i ask u abt my code...wats the problem is actually???the logic??or anything that not suit to use inside my code???

i found out that $row2 will only remember the first record that i inserted...and oso it will compare the first record...

can u pls giv me some tips??


02-22-2007, 11:41 AM
you need to loop through your $row using a do while loop

02-22-2007, 12:08 PM
i try to use do while loop at $row...but the result is still the same....there r even more redundancy results???

is it sth wrong with my $row2????

thank u

02-22-2007, 12:14 PM
first you need to troubleshoot

start by echoing your $query1 var so you can see the insert SQL in action

02-22-2007, 12:22 PM
if there r 2 records inserted when user browse the page....the 2nd record will always insert whenever user browse the page...

then if there r only 1 record inserted....then no prob dee...

is it $row2 will only remember the first inserted record???


02-22-2007, 12:25 PM
yeah!!!i tried dee....i hav declared a variable to store the patient ID...then i tried to echoing the result....it is only store the first patient ID....

02-22-2007, 12:26 PM
ok lets start from the begining

what are you trying to do

02-22-2007, 01:26 PM
actually $row is to show all the records fr tblpatient....then i need to check the last treatment date...whether it is more than 6 months...if it is...i need to insert it into another table call tbloverdue....coz i need to send an email to inform the patients....after i send email...it will automatically delete the record in tbloverdue....then the prob u should know rite???

thanks a lot

02-22-2007, 03:29 PM
this is a major request and your script will have to be rewritten

its not as bad as someone asking "i want to write a site like youtube. Please post the code" but it will take time.

i just dont have the time at the minute

You could take an hour or 2 and follow the tutorials then you will really know what you are doing and i can help better