PDA

View Full Version : Lỗi Notice: Undefined index: check in



joasny
18-04-2012, 02:07 PM
Như title mình có đoạn code sau


<?php
include('config.php');
if($_POST['check'])
{
$loginName = $_POST["loginName"];
$result = mysql_query("SELECT * FROM account WHERE loginName LIKE '$loginName' ");
if (mysql_num_rows($result) > 0) {echo "<font color='Red'>Tài khoản này đã được người khác sử dụng.</font> <br /> <a href='javascript:history.go(-1)'>Trở lại</a> <a href='javascript:window.close()'>Thoát</a>";exit;}
else {echo "<font color='Green'>Bạn có thể sử dụng tài khoản này.</font> <br /> <a href='javascript:history.go(-1)'>Trở lại</a> <a href='javascript:window.close()'>Thoát</a>";exit; }
}
?>
<form method="post" action=""><br />
Nhập tài khoản:
<input name="loginName" type="text" /><br />
<input type="submit" name="check" value="Check"/>
<input type="reset" name="reset" value="Reset" /><br />
</form>

Mỗi khi chạy thì nó vẫn hoạt động bình thường nhưng luôn hiện ra

Notice: Undefined index: check in **** on line 8
Ai chỉ mình fix với (bik file cònig.php là truy cập dữ liệu MySQL)

HTT.itvn
18-04-2012, 08:44 PM
Em chú ý là khi khai báo action trong form thì không nên để trống mà hãy đặt một biến nào đó để xử lý cục bộ hơn là toàn cục. Nếu xử lý action nào cũng để trống thì dĩ nhiên là nó không biết lựa chọn GET nào để xử lý. Ví dụ anh có file check.php với code sau nhé :


<?phpinclude('config.php');
if($_GET['kiemtra'] == "ten-dang-nhap")
{
$loginName = $_POST["loginName"];
$result = mysql_query("SELECT * FROM account WHERE loginName LIKE '{$loginName}' ");
if (mysql_num_rows($result) > 0)
{
echo "<font color='Red'>Tài khoản này đã được người khác sử dụng.</font> <br />
<a href='javascript:history.go(-1)'>Trở lại</a> <a href='javascript:window.close()'>Thoát</a>";
exit();
}
else
{
echo "<font color='Green'>Bạn có thể sử dụng tài khoản này.</font> <br /> <a href='javascript:history.go(-1)'>Trở lại</a>
<a href='javascript:window.close()'>Thoát</a>";
exit();
}
}
?>
<form method="post" action="./check.php?kiemtra=ten-dang-nhap"><br />
Nhập tài khoản:
<input name="loginName" type="text" /><br />
<input type="submit" name="submit" value="Check"/>
<input type="reset" name="reset" value="Reset" /><br />
</form>

Chú ý dòng màu đỏ . Bình thường khi lưu giá trị string vào CSDL người ta thường lưu dưới dạng addslashes. Nếu code trên không được thì em thay thế code màu đỏ như sau :

$loginName = addslashes($_POST["loginName"]);