我有一个表单需要在将表单数据输入到数据库之前进行验证。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>";
}
感谢很多:)
[Bobby Tables](http://xkcd.com/327/)警报! – awm 2011-05-29 10:48:54
哈哈多数民众赞成在良好的 - 但没有太多的帮助大声笑 - 这是一个系统的发展,所以不应该在表格中有任何学生记录,直到他们注册此在线表单 - 但没有认识到,与此代码,如果用户名不存在... – 2011-05-29 11:03:35
这不是一个独立的代码片段;你在某处放错了'}'。检查这不是问题。 – Piskvor 2011-05-29 11:13:35