SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    mysql affected rows related question

    Hai folks

    why my script does not show "Record already exist" message when a record already exist?


    Code:
    $query="SELECT * FROM by_nation WHERE pid='$pid' and nation='$nation' and required='$required'";
    if ($result=mysql_query($query) or die (mysql_error())); 
    if(mysql_num_rows($result)<1){
    	$query = "INSERT into by_nation VALUES (
    			'$pid',
    			'$nation',
    			'$required',
    			'$achived',
    			'$balance'
    			)"; 
    			
    	if ($result=mysql_query($query) or die (mysql_error()));
    	if(mysql_affected_rows()>0){
    	   echo "Record added successfully";
    	}else{
    	   echo "Record already exist.";
    	}   
    }

  2. #2
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    sorry folks,
    i messed up a bit and i have now corrected the code and works fine.

    PHP Code:
    if(mysql_num_rows($result)<1){
        
    $query "INSERT into by_nation VALUES (
                '
    $pid',
                '
    $nation',
                '
    $required',
                '
    $achived',
                '
    $balance'
                )"

                
        if (
    $result=mysql_query($query) or die (mysql_error()));
        if(
    mysql_affected_rows()>0){
           echo 
    "Record added successfully";
        }else{
           echo 
    "Record cannot be updated.";
        }

    }else{
       echo 
    "Record already exist";



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •