Page 1 of 1

help with code

Posted: Sat Nov 01, 2008 1:13 am
by zed420
Hi All
Can someone please help me with this query, 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 query below I’m getting error message both times whether he inserts Right or Wrong Acc no. Some help will be greatly appreciated.

Code: Select all

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 .....
}
}
}
}
 
Thanks Zed :banghead:

Re: help with code

Posted: Sun Nov 02, 2008 2:11 am
by bmoyles0117

Code: Select all

if(isset($_POST['user_id'])) {
    $user = new stdClass;
    $user->id = mysql_real_escape_string($_POST['user_id']);
    $user->idQuery = mysql_query("SELECT `id` FROM `user` WHERE `id` ='{$user->id}'")
        or die(mysql_error());
 
    // If the user was found,
    if(mysql_num_rows($result) < 1)
        throw new Exception("Your Account number was NOT found in our database!");
    else {
        $name = mysql_real_escape_string($_SESSION['name']);
        $user->usernameQuery = mysql_query("SELECT `id` FROM `user` WHERE `username` = '$name'")
            or die("Couldn't execute query for collecting your data.");
        if(mysql_num_rows($user->usernameQuery) < 1)
            throw new Exception("Sorry your inserted Account no. Does Not match with your username");
        else
            throw new Exception("Sorry your inserted Account no. Does Not match with your username");
    }
}
There is my rendition, quickly compiled so I apologize for any errors but they should be minute. You should remain somewhat consistent in your sql query calls however I did use your calls.

Re: help with code

Posted: Sun Nov 02, 2008 2:55 am
by pcoder
What you are trying to do in the line number 13?