By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

php mysql issue.....if a specific field exists

omerbutt
100+
P: 638
hi there i am new to php ,have been working in asp classic and access but now have switched to php and mysql,and for the practice sake i started to convert my asp and access based projects into php and mysql, i have a inventory system in which i have a stock entry Portion which has aloots of fields concerning to the codeno,partno, auto , product,quantity, size, height and lots of other fields too but our main concern is with these two
"CODENO and PARTNO
sample values for these tow are here
codeno--------part no
1000--------CSV-3345
1000--------CSV-3346
1001--------CSV-3345

by looking at the values above you can figure outsome how that the values for the code no can be repeated more than once for eg you can enter part nos for the CODENO=1000 as many times as u want lets say partno=CSV-3345 but you cannot enter the existing partno for the codeno=1000 twice means the partno=csv-3345 can not be entered twice under codeno=1000 but the same partno=CSV-3345 can be entered for another code no say
"1001"in asp and access what i did for checking if the record exists this is what i did
Expand|Select|Wrap|Line Numbers
  1. sqlf="SELECT Stk_items.Prt_no from Stk_items where Stk_items.Code_no="&lngcodeno
  2. rf.Open sqlf, connf, 3
  3. foundit=False
  4. do until rf.EOF OR foundit //runs until foundit becomes true
  5.   if (StrComp(rf.Fields("Prt_no"), partno, vbTextCompare) = 0) then                   foundIt=True //assign true to foundit 
  6. else
  7. rf.MoveNext //else move to next record
  8. End if 
  9. Loop
  10. rf.Close
  11. set rf = Nothing
  12. set connf= Nothing    
  13.  
this runs accurately perfect foor asp and access but in php the case is different what i have done in my php code is here
Expand|Select|Wrap|Line Numbers
  1.     $hostname="localhost";
  2.     $username="root";
  3.     $password="66456";
  4.     $database="db1";
  5.  
  6. $conn_stk = mysql_pconnect($hostname, $username, $password) or 
  7. trigger_error(mysql_error(),E_USER_ERROR);
  8.     mysql_select_db($database, $conn_stk);
  9.  
  10.     $codeno=$_POST["code_no"];
  11.     $prtno=$_POST["prt_no"];
  12.     $prd=$_POST["prd"];
  13.     $brand=$_POST["brand"];
  14.     $auto=$_POST["auto"];
  15.     $uprc=$_POST["unit_price"];
  16.     $qtty=$_POST["qtty"];
  17.     $sroom=$_POST["show_room"];
  18.     $st1=$_POST["store_1"];
  19.     $st2=$_POST["store_2"];
  20.     $st3=$_POST["store_3"];
  21.     $size=$_POST["size"];
  22.     $height=$_POST["height"];
  23.     $od1=$_POST["out_d1"];
  24.     $od2=$_POST["out_d2"];
  25.     $id1=$_POST["in_d1"];
  26.     $id2=$_POST["in_d2"];
  27.     $trep=$_POST["t_rep"];
  28.     $rp1=$_POST["rp_no1"];
  29.     $rp2=$_POST["rp_no2"];
  30.     $rp3=$_POST["rp_no3"];
  31.     $rp4=$_POST["rp_no4"];
  32.     $rp5=$_POST["rp_no5"];
  33.     $rp6=$_POST["rp_no6"];
  34.  
  35.  
  36.     $sqlf="select Prt_no from stk_items where Code_no='$codeno'";
  37.     $rs=mysql_query($sqlf) or die(mysql_error().'<br />'.$sqlf);
  38.  
  39.     $foundit=false;
  40.  
  41.     if(mysql_num_rows($rs))
  42.     {
  43.         while($f=mysql_fetch_array($rs));
  44.             {    
  45.                 $ret=strcmp($f['Prt_no'],$prtno);                
  46.                 if($ret==0)
  47.                 {
  48.                     $foundit=true;
  49.                 }
  50.                 else
  51.                 {}                
  52.             }
  53.     }
  54.  
BUT THE PROB IS THAT IF I ENTER THE PART NO WHICH ALREADY EXISTS IT TELLS THAT THE VALUE EXISTS BUT IF I ENETER ANY OTHER PART NO THAT DOESNOT EXIST IT JUST HANKS
ANY SUGGESTIONS WOULD BE HIGHLY APPRECIATED
REGARDS,
OMER
Feb 13 '08 #1
Share this Question
Share on Google+
1 Reply


dlite922
Expert 100+
P: 1,584
hi there i am new to php ,have been working in asp classic and access but now have switched to php and mysql,and for the practice sake i started to convert my asp and access based projects into php and mysql, i have a inventory system in which i have a stock entry Portion which has aloots of fields concerning to the codeno,partno, auto , product,quantity, size, height and lots of other fields too but our main concern is with these two
"CODENO and PARTNO
sample values for these tow are here
codeno--------part no
1000--------CSV-3345
1000--------CSV-3346
1001--------CSV-3345

by looking at the values above you can figure outsome how that the values for the code no can be repeated more than once for eg you can enter part nos for the CODENO=1000 as many times as u want lets say partno=CSV-3345 but you cannot enter the existing partno for the codeno=1000 twice means the partno=csv-3345 can not be entered twice under codeno=1000 but the same partno=CSV-3345 can be entered for another code no say
"1001"in asp and access what i did for checking if the record exists this is what i did
Expand|Select|Wrap|Line Numbers
  1. sqlf="SELECT Stk_items.Prt_no from Stk_items where Stk_items.Code_no="&lngcodeno
  2. rf.Open sqlf, connf, 3
  3. foundit=False
  4. do until rf.EOF OR foundit //runs until foundit becomes true
  5. if (StrComp(rf.Fields("Prt_no"), partno, vbTextCompare) = 0) then foundIt=True //assign true to foundit 
  6. else
  7. rf.MoveNext //else move to next record
  8. End if 
  9. Loop
  10. rf.Close
  11. set rf = Nothing
  12. set connf= Nothing    
  13.  
this runs accurately perfect foor asp and access but in php the case is different what i have done in my php code is here
Expand|Select|Wrap|Line Numbers
  1.     $hostname="localhost";
  2.     $username="root";
  3.     $password="66456";
  4.     $database="db1";
  5.  
  6. $conn_stk = mysql_pconnect($hostname, $username, $password) or 
  7. trigger_error(mysql_error(),E_USER_ERROR);
  8.     mysql_select_db($database, $conn_stk);
  9.  
  10.     $codeno=$_POST["code_no"];
  11.     $prtno=$_POST["prt_no"];
  12.     $prd=$_POST["prd"];
  13.     $brand=$_POST["brand"];
  14.     $auto=$_POST["auto"];
  15.     $uprc=$_POST["unit_price"];
  16.     $qtty=$_POST["qtty"];
  17.     $sroom=$_POST["show_room"];
  18.     $st1=$_POST["store_1"];
  19.     $st2=$_POST["store_2"];
  20.     $st3=$_POST["store_3"];
  21.     $size=$_POST["size"];
  22.     $height=$_POST["height"];
  23.     $od1=$_POST["out_d1"];
  24.     $od2=$_POST["out_d2"];
  25.     $id1=$_POST["in_d1"];
  26.     $id2=$_POST["in_d2"];
  27.     $trep=$_POST["t_rep"];
  28.     $rp1=$_POST["rp_no1"];
  29.     $rp2=$_POST["rp_no2"];
  30.     $rp3=$_POST["rp_no3"];
  31.     $rp4=$_POST["rp_no4"];
  32.     $rp5=$_POST["rp_no5"];
  33.     $rp6=$_POST["rp_no6"];
  34.  
  35.  
  36.     $sqlf="select Prt_no from stk_items where Code_no='$codeno'";
  37.     $rs=mysql_query($sqlf) or die(mysql_error().'<br />'.$sqlf);
  38.  
  39.     $foundit=false;
  40.  
  41.     if(mysql_num_rows($rs))
  42.     {
  43.         while($f=mysql_fetch_array($rs));
  44.             {    
  45.                 $ret=strcmp($f['Prt_no'],$prtno);                
  46.                 if($ret==0)
  47.                 {
  48.                     $foundit=true;
  49.                 }
  50.                 else
  51.                 {}                
  52.             }
  53.     }
  54.  
BUT THE PROB IS THAT IF I ENTER THE PART NO WHICH ALREADY EXISTS IT TELLS THAT THE VALUE EXISTS BUT IF I ENETER ANY OTHER PART NO THAT DOESNOT EXIST IT JUST HANKS
ANY SUGGESTIONS WOULD BE HIGHLY APPRECIATED
REGARDS,
OMER

Use mysql_fetch_assoc instead of array.

or change it to access it like $f[0]

also change $ret==0 to $ret===0 (check type)

then try again.

PS, you don't need that extra else.
Feb 20 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.