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. 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
I think this line was your problem: //if (mysql_num_rows($result) != 'user_id') { if (mysql_result($result, 0) != $user_id) { PHP:
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:
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