june_c21 Posted January 22, 2008 Share Posted January 22, 2008 when i run this code, it keep on looping and insert data into my database. What's wrong? <title>E Claim</title> <?php session_start(); $host = 'localhost'; $user = 'root'; $password = 'admin'; $dbase = 'eclaim'; $dblink = mysql_connect($host,$user,$password); mysql_select_db($dbase,$dblink); $staff_no = $_POST['staff_no']; $address1 =$_POST['address1']; $address2 =$_POST['address2']; $address3 =$_POST['address3']; $jenis_kenderaan =$_POST['jenis_kenderaan']; $kenderaan_no =$_POST['kenderaan_no']; $cc =$_POST['cc']; $status =$_POST['status']; $tarikh =$_POST['tarikh']; $perjalanan =$_POST['perjalanan']; $masa =$_POST['masa']; $km =$_POST['km']; $tol =$_POST['tol']; $parking =$_POST['parking']; $elauan =$_POST['elauan']; $tujuan =$_POST['tujuan']; while (tarikh !=NULL) { $query= "INSERT INTO claim (staff_no,address1,address2,address3,jenis_kenderaan,kenderaan_no,cc,status,tarikh,perjalanan,masa,km,tol,parking,elauan,tujuan) VALUES ('$staff_no', '$address1','$address2','$address3','$jenis_kenderaan','$kenderaan_no','$cc','$status','$tarikh','$perjalanan','$masa','$km','$tol','$parking','$elauan','$tujuan') "; $result = mysql_query($query,$dblink); } $query1 = "Select claim.staff_no,user.staff_no,user.department from claim,user where user.staff_no = user.staff_no "; $result1 = mysql_query($query1,$dblink); $myrow1 = mysql_fetch_row($result1); $no = "EC/ "; $department =$myrow1[2] ; if (department == 'Generating Facility 1') { $body="<em>HTML</em> formatted <strong>Message</strong>"; //$headers = "From: administrator@eclaim.com \r\n"; $headers = "Content-Type: text/html; charset=ISO-8859-1 "; $headers .= "MIME-Version: 1.0 "; mail("june@kaparenergy.com.my", "Lubrication", $body, $headers); $query2 = "Select gf1,id from runningno "; $result2 = mysql_query($query2,$dblink); $myrow2 = mysql_fetch_row($result2); //echo "00"."$myrow2[0]"." / "; $query3 ="update runningno set id=id+1, gf1=gf1+1"; $result3 = mysql_query($query3,$dblink); $no = $no . "GF1 / 0".$myrow2[0]. "/0".$myrow2[1]; } else { } while (tarikh !=NULL) { $query13 = "UPDATE claim SET tran_id='$no' WHERE id = '$id' "; $result13 = mysql_query($query13,$dblink); } $query40= "Select tran_id from claim "; $result40 = mysql_query($query40,$dblink); $myrow40 = mysql_fetch_row($result40); echo "$myrow40[0]"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/ Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 Just at first glance I noticed that if this is true: while (tarikh !=NULL) { It'll never stop. Why would you use a loop here, wouldn't an if statement be more practical? Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445839 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 i want to insert multiple row of data into the database. when it come to 'tarikh is NULL (empty) it will stop the loop. how to write that? Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445845 Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 Is the post variable an array? Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445846 Share on other sites More sharing options...
pdkv2 Posted January 22, 2008 Share Posted January 22, 2008 I guess you should check again for the following linr while (tarikh !=NULL) { the "tarikh" should be "$tarikh" as this is a variable. Regards Sharad Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445848 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 i change it to $tarikh but it doesnt insert the data into the database. Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445853 Share on other sites More sharing options...
pdkv2 Posted January 22, 2008 Share Posted January 22, 2008 Use while (!is_null($tarikh)) { Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445855 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 i try to use while (!is_null($tarikh)) the same problem appear where it keep on looping the data. Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445858 Share on other sites More sharing options...
awpti Posted January 22, 2008 Share Posted January 22, 2008 Well.. $tarikh = $_POST['tarikh']; $tarikh will never == null Nothing is setting it to null. You have no constraints and zero logic in this script. I think you need to re-approach this script. We're not going to write it for you. Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445860 Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 Exactly what I said above, which is why I asked if the post variable is an array Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445864 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 i'm not asking for writing out the code. i just want a guide of my error only as i new with php. i need to take data (tarikh) from a form that contain 10 lines. if user enter only 5 lines then it will store 5 lines in the database instead of 10lines Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445868 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 Exactly what I said above, which is why I asked if the post variable is an array err.. i not sure.. Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445869 Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 So it is an array. Try this: foreach($tarikh as $item){ // run queries } Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445870 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 $item refer to ?? Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445872 Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 $item refers to each individual posted $tarikh variable Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445874 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 so i need to declare $item = ?? sorry, i bit stupid..... need more explanation and guide. hope u guys don't mind Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445875 Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 No, it's being declared in the foreach statement. Look here for a tutorial. http://us2.php.net/manual/en/control-structures.foreach.php Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445878 Share on other sites More sharing options...
june_c21 Posted January 22, 2008 Author Share Posted January 22, 2008 this warning i get when i put it in Warning: Invalid argument supplied for foreach() in /var/www/Web/eclaim/add.php on line 27 Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-445883 Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 What's line 27? Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-446185 Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 Backup a little here. What kind of form element is tarikh? A textarea, checkboxes, something else? Also, at the very beginning of your PHP code, try putting the following lines. Then fill out the form, click submit, and see what it prints out. print_r($_POST);exit; Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-446188 Share on other sites More sharing options...
p2grace Posted January 22, 2008 Share Posted January 22, 2008 That's an excellent idea, post the content of the print_r too please Quote Link to comment https://forums.phpfreaks.com/topic/87169-error/#findComment-446193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.