2011-05-29 63 views
1

我有一个表单需要在将表单数据输入到数据库之前进行验证。php和mysql_num_rows没有检测到数据库中没有东西

它通过用户mysql_num_rows函数检查用户名是否已经存在。但我似乎无法得到它的工作。测试时,不会添加新的用户名。

这里是正在使用的全码:

<?php 
session_start(); 

include("databaseConnect.php"); 
// Insert a row of information into the table "example" 


// check if username is already in database 
if(mysql_num_rows(mysql_query("SELECT userName FROM registeredUsers WHERE userName =  '$_POST[userName]'"))){ 
echo "Username: ". $_POST[userName]." already exists in the Database<br><br>"; 
    echo "You will be redirect back to the form in 5 seconds"; 
$ref = $_SERVER['HTTP_REFERER']; 
header('refresh: 5; url='.$ref); 

//check if hemis is already in database 
}elseif(mysql_num_rows(mysql_query("SELECT hemis FROM registeredUsers WHERE hemis = '$_POST[hemis]'"))){ 
echo "Student [Hemis] Number: ". $_POST[hemis]." already exists in the Database<br><br>"; 
echo "You will be redirect back to the form in 5 seconds"; 
$ref = $_SERVER['HTTP_REFERER']; 
header('refresh: 5; url='.$ref); 


// if all the conditions above are fine, it will insert the data to MySQL 
}else{ 
    mysql_query("INSERT INTO registeredUsers 
(firstName, lastName, hemis, userName, MAC) VALUES('$_POST[firstName]', '$_POST[lastName]', '$_POST[hemis]', '$_POST[userName]', '$_POST[mac]') ") 
or die(mysql_error()); 

echo "Data Inserted! <br><br>"; 
} 

感谢很多:)

+5

[Bobby Tables](http://xkcd.com/327/)警报! – awm 2011-05-29 10:48:54

+0

哈哈多数民众赞成在良好的 - 但没有太多的帮助大声笑 - 这是一个系统的发展,所以不应该在表格中有任何学生记录,直到他们注册此在线表单 - 但没有认识到,与此代码,如果用户名不存在... – 2011-05-29 11:03:35

+0

这不是一个独立的代码片段;你在某处放错了'}'。检查这不是问题。 – Piskvor 2011-05-29 11:13:35

回答

1

我会完全重写此。它受SQL注入的影响,效率低下并且有点过于简单。此外,您通常更适合使用PHP mysqli extension

另外,请确保将$ _POST变量名称括在引号中。你把它们写成常量,而不是字符串。 (除非你在表示字符串值代码的其他地方定义的常量,这是一个错误。打开PHP警告当你正在开发)。

$safe_username = mysqli_real_escape_string($_POST['userName']); 
$sql = "SELECT userName FROM registeredUsers WHERE userName='$safe_username' LIMIT 1"; 
$result = mysqli_query($database_connection, $sql); 
if (mysqli_num_rows($result)) 
{ 
    // username already found code 
    mysqli_free_result($result); 
} 
else 
{ 
    $safe_hemis = mysqli_real_escape_string($_POST['hemis']); 
    $sql = "SELECT hemis FROM registeredUsers WHERE hemis='$safe_hemis' LIMIT 1"; 
    // Side note, LIMIT 1 tells the database engine to stop looking after it's found one hit. More efficient as you're only looking for a Boolean value anyway. 
    $result = mysqli_query($database_connection, $sql); 
    if (mysqli_num_rows($result)) 
    { 
     // hemis found code 
     mysqli_free_result($result); 
    } 
} 

你也许可以从该弄清楚的休息。

请验证并退出所有输入。验证包括检查是否完整 - 数据范围内的数据(字符串长度,数值范围等)等。所有输入都是邪恶的!

你真的不想依赖于HTTP_REFERER。用户代理不总是传递引用者。

另外,我知道这不是什么大不了的事,但是使用CSS而不是<br>。如果您使用的是XHTML文档类型,则必须正确关闭所有标签,因此<br>将变为<br />。无论如何,这是个好主意。

+0

+1我也会进行一些彻底的重构,如果不是从头开始。 :) – GolezTrol 2011-05-29 11:55:34

1

最好也检查mysql_query的结果。它可能会返回一个可以获取行数的结果集,但查询失败时可能会返回false。在这种情况下,你不会有结果集,并且mysql_count_rows将失败。这个失败你解释为0行。

除了Matty给你的所有建议外,我还会做一些额外的检查和严格的检查。

if ($result = mysql_query('....') === false) 
{ 
    die('Your query failed in the first place. Error: ' . mysql_error()); 
} 

有许多改进,你可以(像在查询和使用等计数),但我想你应该至少有这类检查。它会帮助你理解实际发生的问题,而不是猜测。它会为您的初学者或有经验的程序员节省大量的调试时间。

+0

这是我忘了写在我的答案中的一件事。 '$ result'可能为NULL,而不是“strict”false,这应该进行测试。最好的方法是测试评估为true的值,并将'die()'放入'else'子句中。 – Matty 2011-05-29 12:00:18

+0

如果你发现mysql_query返回NULL,你可能发现了一个bug。它应该为返回数据的语句(比如select)返回一个false的资源,并且它应该返回true或false,而不是像更新那样的stamenets。它在文档中没有说NULL是有效的结果。 – GolezTrol 2011-05-30 06:40:54

+0

不,你说得对 - 再次检查文件! – Matty 2011-05-30 12:15:12