 |
มีปัญหาเรื่องระบบสมัครสมาชิกครับ ข้อมูลซ้ำแต่มันก็ยังคงเก็บลงฐานข้อมูลอยู่ดี |
|
 |
|
|
 |
 |
|
ตอนนี้มีปัญหาเรื่องระบบสมาชิกครับ คือผมมีฐานข้อมูลชื่อ fuckchat กับตารางชื่อ fuckuser ซึ่งประกอบไปด้วยสามแถวคือ username, password กับ name ทีนี้ จากโค้ดด้านล่าง จะเห็นว่าผมทำ query กับข้อมูลในตาราง รวมถึงนับแถวเวลามีการกรอกข้อมูลลงในฟอร์มไปแล้ว ทีนี้ตามหลักแล้วถ้ากรณีมีชื่อซ้ำกันก็จะไม่เอาข้อมูลเก็บลงฐานข้อมูล แต่ผมมีปัญหาอยู่ที่ว่า ตอนนี้พอผมลองพิมพ์ข้อมูลซ้ำปุ๊บ ไอ้หน้าจอที่บอกว่า ข้อมูลซ้ำ (ในที่นี้คือ username) มันก็ขึ้นมาซึ่งถือว่าปกติ เพราะถ้ามูลซ้ำมันต้องแจ้งมาให้ผู้ใช้ทราบอยู่แล้ว แต่ปัญหาจริงๆมันอยู่ที่ว่าระบบของไฟล์นี้ก็ยังคงเอาข้อมูลเก็บลงฐานข้อมูลอยู่ดี ทั้งๆที่ขึ้นว่าข้อมูลซ้ำแล้ว รบกวนพี่ช่วยวิเคราะห์โค้ดที่ผมเขียนขึ้นมาให้หน่อยครับว่าผมต้องแก้ตรงไหน
Code (PHP)
<?php
$host = "127.0.0.1";
$user = "root";
$passwd = "1234";
$dbname = "fuckchat";
mysql_connect($host,$user,$passwd) or die("Can not connect to the server");
mysql_select_db($dbname) or die("Can not connect to the database");
$sql="insert into `fuckuser` (`username`,`password`,`name`)
values('$username','$password','$name')";
mysql_query("set NAMES UTF8");
$sqlquery=mysql_db_query($dbname, $sql);
$checkadd = "select*from `fuckuser` where username='$username'";
$result = mysql_db_query($dbname, $checkadd);
$num=mysql_num_rows($result);
if ($num==1)
{
print"<br>Your name has been successfully added";
}elseif($num>=1)
{
print"<br>Your name has already been used ";
}else
{
print"Error!!!";
};
mysql_close();
?>
ขอบคุณมากครับ
Tag : PHP, MySQL, Ajax, WebService, LINQ
|
|
 |
 |
 |
 |
Date :
2013-01-10 00:47:39 |
By :
aslscarecrow |
View :
901 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
elseif($num>=1) <<< ลองแก้เป็น elseif($num>1) ดูครับ
|
 |
 |
 |
 |
Date :
2013-01-10 01:45:55 |
By :
continue |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับผม ขอบคุณครับ ขอลองดูก่อนนะ
|
 |
 |
 |
 |
Date :
2013-01-10 02:30:12 |
By :
aslscarecrow |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
$host = "127.0.0.1";
$user = "root";
$passwd = "1234";
$dbname = "fuckchat";
mysql_connect($host, $user, $passwd) or die("Can not connect to the server");
mysql_select_db($dbname) or die("Can not connect to the database");
mysql_query("set NAMES UTF8");
$checkadd = " SELECT * FROM 'fuckuser' WHERE username = '".$username."' ";
$result = mysql_db_query($dbname, $checkadd) or die (mysql_error());
$num = mysql_num_rows($result);
if($num<=0){
$sql=" INSERT INTO fuckuser ('username', 'password', 'name') ";
$sql.=" VALUES ";
$sql.=" ('".$username."', '".$password."', '".$name."') ";
$qr = mysql_db_query($dbname, $sql) or die (mysql_error());
print"<br>Your name has been successfully added";
}else{
print"<br>Your name has already been used ";
};
mysql_close();
?>
|
 |
 |
 |
 |
Date :
2013-01-10 08:33:04 |
By :
popnakub |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|