2012-11-16 4 views
0

저는 SQL과 PHP를 처음 접했습니다.PHP 로그인 스크립트 SQL 오류

간단한 로그인 스크립트를 작성하려고합니다. HTML 문서에 양식이 있는데 필요한 2 개의 변수에 올바른 데이터를 게시했지만 SQL이 실행될 때 스크립트가 실패합니다 ...

또한 mysqlWorkbench에서 SQL을 테스트했으며 내가 원하는 결과 ???

도와주세요.

<?PHP 

$odbc = mysql_connect('localhost', 'root', '') or die ("could not connect to database"); 
mysql_select_db('examresults', $odbc) or die("Could not find database"); 

// username and password sent from form 
$username=$_POST['username']; 
$password=$_POST['password']; 

$sql='SELECT * FROM tuser where username = '.$username.' and password = '.$password.''; 

$result = mysql_query($sql, $odbc) or die ("Error in SQL"); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

//If result matched username and password, table row must only equal 1 row 
if($count==1) 
{ 
    header("location:exammenu.php"); 
} 
else 
{ 
    echo 'username and password do not match'; 
} 
?> 
+3

새로운 기능이라면 mysql_connect를 사용하지 말고 (익숙한 경우) mysqli를 찾아 보시기 바랍니다. http://www.php.net/manual/en/function.mysql-connect.php – Lucas

+0

mysql을 사용해야합니다. – Matt

+1

어떤 오류가 발생합니까? – Lucas

답변

2

참고 : mysql_* 기능은 더 이상 사용되지 않으므로 더 이상 사용하면 안됩니다. 귀하의 코드는 SQL Injection에 취약합니다.

"Error in SQL"대신 "mysql_error"을 사용하면보다 자세한 SQL 오류 메시지를 얻을 수 있습니다. 그러나 쿼리에서 문자열 주위에 " "을 넣는 것을 잊었 기 때문에 실패 할 가능성이 큽니다.

$sql='SELECT * FROM tuser where username = "'.$username.'" and password = "'.$password.'"'; 
+0

감사합니다 yeh ive가 문자열 대신 정수로 작업하고 문자열을 잊어 버렸습니다. " – Matt

2

쿼리는 다음과 같이해야한다 :

$sql='SELECT * FROM tuser where username = "'.$username.'" and password = "'.$password.'"'; 
2

당신이 정말로 적어도 입력을 소독 mysql, 를 사용해야 위하여려고하는 경우에

여기 내 스크립트입니다. 또한 $ sql 변수의 따옴표를 주목하십시오. (테스트 것은 아니지만)이 작동합니다 : 나는 쉽게 이러한 오류를 발견 할 수 있도록 SQL 문을 포맷 할 sprintf을 사용하는 것이 좋습니다

<?PHP 

$odbc = mysql_connect('localhost', 'root', '') or die ("could not connect to database"); 
mysql_select_db('examresults', $odbc) or die("Could not find database"); 

// username and password sent from form 
$username=mysql_real_escape_string($_POST['username'], $odbc); 
$password=mysql_real_escape_string($_POST['password'], $odbc); 

$sql=sprintf('SELECT * FROM tuser where username = "%s" and password = "%s"', $username, $password); 

$result = mysql_query($sql, $odbc) or die ("Error in SQL"); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

//If result matched username and password, table row must only equal 1 row 
if($count==1) 
{ 
    header("location:exammenu.php"); 
} 
else 
{ 
    echo 'username and password do not match'; 
} 

.

1

이 코드를 사용해보십시오. 나는 그것이 올바르게 작동 할 것이라고 생각한다.

<?PHP 

$odbc = mysql_connect('localhost', 'root', '') or die ("could not connect to database"); 
mysql_select_db('examresults', $odbc) or die("Could not find database"); 

// username and password sent from form 
$username=$_POST['username']; 
$password=$_POST['password']; 

$sql="SELECT * FROM tuser where username = '".$username."' and password = '".$password."'"; 

$result = mysql_query($sql, $odbc) or die ("Error in SQL"); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

//If result matched username and password, table row must only equal 1 row 
if($count==1) 
{ 
    header("location:exammenu.php"); 
} 
else 
{ 
    echo 'username and password do not match'; 
} 
?>