Code help

Discussion in 'PHP' started by zed420, Nov 1, 2008.

  1. #1
    Hi All
    Can someone please help me with this code, there are two MySQL tables ‘user_tb’ and ‘job_tb’ id is the primary key in user_tb and user_id is the foreign key in job_tb. A user populates the user_id by form as his Acc no. All I’m trying to do is to prevent him inserting the wrong Acc no (user_id) if he does an error message pop up. With code below I’m getting error message both times whether he inserts Right or Wrong Acc no.:confused: Some help will be greatly appreciated.

     if (isset($_POST['user_id'])) {
    	 $user_id= mysql_real_escape_string($_POST['user_id']); 
         $query = "SELECT id FROM user WHERE id ='$user_id'";
         $result = mysql_query($query)or die(mysql_error());
                      // If the user was found, 
         if (mysql_num_rows($result) < 1) {
     	 error_message("Your Account number was NOT found in our database!");   
    		 }else{
    		if ($name = $_SESSION['name']){
    	$query = "SELECT id FROM user WHERE username = '$name'";
    	$result = mysql_query($query)
           or die ("Couldn't execute query for collecting your data.");
         if (mysql_num_rows($result) != 'user_id') {
     	 error_message("Sorry your inserted Account no. Does Not match with your username");   
    		 }else{
    Query= INSERT .....
    }
    }
    }
    }
    
    Code (markup):
    Thanks
    Zed
     
    zed420, Nov 1, 2008 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think this line was your problem:

    
    
    //if (mysql_num_rows($result) != 'user_id') {
    
    if (mysql_result($result, 0) != $user_id) {
    
    PHP:
     
    LogicFlux, Nov 1, 2008 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    First I would organize your code to be easier to read;
    Check this place out: http://www.phpformatter.com/

    Is `id` a VARCHAR or a INTEGER or a TEXT? Because '' are used for string values and do not put ' ' around integers.

    Also, you single = in a if statement; It has to be a double == to work correctly;

    
    <?php
      if (isset($_POST['user_id']))
      {
          $user_id = mysql_real_escape_string($_POST['user_id']);
          $query = "SELECT `id` FROM `user` WHERE `id` = $user_id limit 1";
          $result = mysql_query($query) or die(mysql_error());
          // If the user was found, 
          if (mysql_num_rows($result) == 0)
          {
              error_message("Your Account number was NOT found in our database!");
          }
          else
          {
              
              if ($name == $_SESSION['name'])
              {
                  $query = "SELECT `id` FROM `user` WHERE `username` = '$name' limit 1";
                  $result = mysql_query($query) or die("Couldn't execute query for collecting your data.");
    
                  if (mysql_num_rows($result) == 0)
                  {
                      error_message("Sorry your inserted Account no. Does Not match with your username");
                  }
                  else
                  {
                      
                      Query = INSERT . ....
                  }
              }
          }
      }
    ?>
    
    PHP:
     
    exodus, Nov 1, 2008 IP
  4. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    THANK YOU LogicFlux.

    I think you're my next best friend LogicFlux I've been at for two days I knew it was that line but couldn't get my head round it. YOU HAVE SOLVED my problem. ($result, 0) What does does this 0 represent??

    Thank you once again
    Zed
     
    zed420, Nov 1, 2008 IP