Jump to content

Need help with code


zed420

Recommended Posts

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. there are no other errors, I have also echo the id from user it bring out the right one for the current session user. 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 .....
      }
    }
  }
}

 

Thanks

Zed

 

 

Link to comment
Share on other sites

if (mysql_num_rows($result) != 'user_id') {

 

u r comparing integer with string...i think u need this.

$query = "SELECT id FROM user WHERE username = '$name' and id = '$user_id'";
$result = mysql_query($query)
       or die ("Couldn't execute query for collecting your data.");
     if (mysql_num_rows($result) < 1) {
	error_message("Sorry your inserted Account no. Does Not match with your username");   
	}else{
Query= INSERT .....

Link to comment
Share on other sites

Your code is confusing from the get-go

 

<?php
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 {
        $row = mysql_fetch_assoc($res);
        if ($row['name'] == $_SESSION['name']) {
            $query2 = "SELECT * FROM user WHERE username = '" . $row['name'] . "'";
            $result2 = mysql_query($query2) or die("Couldn't execute query for collecting your data.");
            $row2 = mysql_fetch_assoc($result2);
            if ($row2['id'] != $user_id) {
                error_message("Sorry your inserted Account number does not match with your username");
            } else {
                #Query = INSERT . . . . .
            }
        }
    }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.