I am trying to check if a numerical code that the user enters has already been submitted. I have already checked that the table exists as the first numerical code, but I keep getting an error when I try to check if the second numerical code exists in the table:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1001' at line 1

I am kind of new to programming and I would really appreciate some help.

// Check if a valid code1 
$table_name = "1001";
$table_found = false;
$result = mysql_query("SHOW TABLES");
while($row = mysql_fetch_array($result)) {
	if($row[0] == $table_name) {
    	     $table_found = true;
  	}
}
if($table_found){
	$code3_check = false;
	$query = "SELECT * FROM 1001"; 
	$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){
		if($code3 == $code_check){
			$code3_check = true;
		}
	}
	if($code3_check){
		// Send user to new page notifying that a valid code has been entered 
	}else{
		// Send user to invalid.php since input is not a valid number
	}			
}else{
	// Send user to invalid.php since input is not a valid number
}

I added the single quotes around the 1001 and I still get the error, but now the error has double quotes around 1001:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1001'' at line 1

// Check if a valid code1 
$table_name = "$code1";
$table_found = false;
$result = mysql_query("SHOW TABLES");
while($row = mysql_fetch_array($result)) {
	if($row[0] == $table_name) {
    	     $table_found = true;
  	}
}
if($table_found){
	$code3_check = false;
	$query = "SELECT * FROM '$table_name'"; 
	$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){
		if($code3 == $code_check){
			$code3_check = true;
		}
	}
	if($code3_check){
		// Send user to new page notifying that a valid code has been entered 
	}else{
		// Send user to invalid.php since input is not a valid number
	}			
}else{
	// Send user to invalid.php since input is not a valid number
}
This article has been dead for over six months. Start a new discussion instead.