Heim >Backend-Entwicklung >PHP-Tutorial >php修改本条数据时提示用户名已存在
php jquery 数据 ajax
看图片太费劲,还是贴文本代码出来吧。
function doSelectNums($tbName, $where) { $sql = "SELECT * FROM " . $tbName . " WHERE " . $where; $result = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($result); return $num; }
function IsExistUser($userName) { $where = "name = '" . $userName . "'"; $clsSql = new DB_Support_jqGrid(); if ($userName != null) $Num = $clsSql -> doSelectNums($this -> tbName, $where); return $Num; }
function IsExistUserName() { $userName = $_POST["name"]; $clsSql = new AdminUser(); $result = $clsSql -> IsExistUser($userName); if ($result == 0) { echo "1"; } else { echo "-9";//用户名已存在 }}
function isExistName(value, colname) { var IsExistName = null; $.ajax({ type : "POST", url : "../php/Interface.php", data : { Index : "IsExistUserName", name : value }, async : false, success : function(data) { IsExistName = data } }); if (IsExistName == "-9") { return [false, "用户名: 已存在"]; } else { return [true, ""]; } }
看图片太费劲,还是贴文本代码出来吧。 OK了 求指点
$sql = "SELECT * FROM " . $tbName . " WHERE " . $where;
后
echo $sql;
看看 sql 串有无问题
当然
success : function(data) {
IsExistName = data
}
要改作
success : function(data) {
IsExistName = data
alert(data);
}
$sql = "SELECT * FROM " . $tbName . " WHERE " . $where;
后
echo $sql;
看看 sql 串有无问题
当然
success : function(data) {
IsExistName = data
}
要改作
success : function(data) {
IsExistName = data
alert(data);
}
修改的时候
执行$sql = "SELECT * FROM " . $tbName . " WHERE " . $where;
有一条数据
如我代码 则返回 1(说明有这个用户名)
所以 修改时会提示 “用户名已存在”
该怎么解决 (只有修改时有问题 ,添加无问题)
把当前修改这条记录的ID传进去
$sql = "SELECT * FROM " . $tbName . " WHERE name='".$username."' and id<>".$id ;
有这个用户才谈得上“修改”
所以你需要把插入和修改分开处理
把当前修改这条记录的ID传进去
$sql = "SELECT * FROM " . $tbName . " WHERE name='".$username."' and id<>".$id ;+1
把当前修改这条记录的ID传进去
$sql = "SELECT * FROM " . $tbName . " WHERE name='".$username."' and id<>".$id ;