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
https://forums.phpfreaks.com/topic/130867-need-help-with-code/
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
https://forums.phpfreaks.com/topic/130867-need-help-with-code/#findComment-679245
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
https://forums.phpfreaks.com/topic/130867-need-help-with-code/#findComment-679959
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.