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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omerbutt
    Contributor
    • Nov 2006
    • 638

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

    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,quantit y, 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
    Code:
    sqlf="SELECT Stk_items.Prt_no from Stk_items where Stk_items.Code_no="&lngcodeno
    rf.Open sqlf, connf, 3
    foundit=False
    do until rf.EOF OR foundit //runs until foundit becomes true
      if (StrComp(rf.Fields("Prt_no"), partno, vbTextCompare) = 0) then                   foundIt=True //assign true to foundit 
    else
    rf.MoveNext //else move to next record
    End if 
    Loop
    rf.Close
    set rf = Nothing
    set connf= Nothing
    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
    Code:
    	$hostname="localhost";
    	$username="root";
    	$password="66456";
    	$database="db1";
    	
    $conn_stk = mysql_pconnect($hostname, $username, $password) or 
    trigger_error(mysql_error(),E_USER_ERROR);
    	mysql_select_db($database, $conn_stk);
    	
    	$codeno=$_POST["code_no"];
    	$prtno=$_POST["prt_no"];
    	$prd=$_POST["prd"];
    	$brand=$_POST["brand"];
    	$auto=$_POST["auto"];
    	$uprc=$_POST["unit_price"];
    	$qtty=$_POST["qtty"];
    	$sroom=$_POST["show_room"];
    	$st1=$_POST["store_1"];
    	$st2=$_POST["store_2"];
    	$st3=$_POST["store_3"];
    	$size=$_POST["size"];
    	$height=$_POST["height"];
    	$od1=$_POST["out_d1"];
    	$od2=$_POST["out_d2"];
    	$id1=$_POST["in_d1"];
    	$id2=$_POST["in_d2"];
    	$trep=$_POST["t_rep"];
    	$rp1=$_POST["rp_no1"];
    	$rp2=$_POST["rp_no2"];
    	$rp3=$_POST["rp_no3"];
    	$rp4=$_POST["rp_no4"];
    	$rp5=$_POST["rp_no5"];
    	$rp6=$_POST["rp_no6"];
    	
    	
    	$sqlf="select Prt_no from stk_items where Code_no='$codeno'";
    	$rs=mysql_query($sqlf) or die(mysql_error().'<br />'.$sqlf);
    	
    	$foundit=false;
    	
    	if(mysql_num_rows($rs))
    	{
    		while($f=mysql_fetch_array($rs));
    			{	
    				$ret=strcmp($f['Prt_no'],$prtno);				
    				if($ret==0)
    				{
    					$foundit=true;
    				}
    				else
    				{}				
    			}
    	}
    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
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Originally posted by omerbutt
    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,quantit y, 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
    Code:
    sqlf="SELECT Stk_items.Prt_no from Stk_items where Stk_items.Code_no="&lngcodeno
    rf.Open sqlf, connf, 3
    foundit=False
    do until rf.EOF OR foundit //runs until foundit becomes true
    if (StrComp(rf.Fields("Prt_no"), partno, vbTextCompare) = 0) then foundIt=True //assign true to foundit 
    else
    rf.MoveNext //else move to next record
    End if 
    Loop
    rf.Close
    set rf = Nothing
    set connf= Nothing
    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
    Code:
    	$hostname="localhost";
    	$username="root";
    	$password="66456";
    	$database="db1";
     
    $conn_stk = mysql_pconnect($hostname, $username, $password) or 
    trigger_error(mysql_error(),E_USER_ERROR);
    	mysql_select_db($database, $conn_stk);
     
    	$codeno=$_POST["code_no"];
    	$prtno=$_POST["prt_no"];
    	$prd=$_POST["prd"];
    	$brand=$_POST["brand"];
    	$auto=$_POST["auto"];
    	$uprc=$_POST["unit_price"];
    	$qtty=$_POST["qtty"];
    	$sroom=$_POST["show_room"];
    	$st1=$_POST["store_1"];
    	$st2=$_POST["store_2"];
    	$st3=$_POST["store_3"];
    	$size=$_POST["size"];
    	$height=$_POST["height"];
    	$od1=$_POST["out_d1"];
    	$od2=$_POST["out_d2"];
    	$id1=$_POST["in_d1"];
    	$id2=$_POST["in_d2"];
    	$trep=$_POST["t_rep"];
    	$rp1=$_POST["rp_no1"];
    	$rp2=$_POST["rp_no2"];
    	$rp3=$_POST["rp_no3"];
    	$rp4=$_POST["rp_no4"];
    	$rp5=$_POST["rp_no5"];
    	$rp6=$_POST["rp_no6"];
     
     
    	$sqlf="select Prt_no from stk_items where Code_no='$codeno'";
    	$rs=mysql_query($sqlf) or die(mysql_error().'<br />'.$sqlf);
     
    	$foundit=false;
     
    	if(mysql_num_rows($rs))
    	{
    		while($f=mysql_fetch_array($rs));
    			{	
    				$ret=strcmp($f['Prt_no'],$prtno);				
    				if($ret==0)
    				{
    					$foundit=true;
    				}
    				else
    				{}				
    			}
    	}
    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_ass oc 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.

    Comment

    Working...