我有一個表單需要在將表單數據輸入到數據庫之前進行驗證。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