我对从数据库以及php中的文本框中重新获取值有一个疑问。
我创建了这样的表,
id blockid flatno
1 1A 100
2 1B 200
3 2A 100
4 2B 200
如果我尝试再次flatno
作为100
插入同一个区块,则必须显示一条错误消息flatno
已存在于blockid 1A
如何这样做..请帮帮我..
答案 0 :(得分:0)
我认为在您的表中不添加唯一索引,因此请添加唯一索引并插入您的数据 并检查数据库错误 或通过代码 $
query = mysql_query("SELECT * FROM tablename WHERE flatno=".$flatno." AND blockid=".$blockid);
$result = mysql_num_rows($query);
if($result > 0){
echo " The Details already exists";
}
答案 1 :(得分:0)
试试这个:
SELECT *
FROM Table1
WHERE flatno=@flatno
AND blockid=@blockid
如果上述查询返回某些内容,则显示错误消息。
echo "Flat No. already exists in this block";
答案 2 :(得分:0)
$query = mysql_query("SELECT * FROM tablename WHERE flatno=".$flatno." AND blockid=".$blockid);
$result = mysql_num_rows($query);
if($result > 0){
echo "already exists";
}
else{
//insert query
}
答案 3 :(得分:0)
Add unique index constraint to the table for block_no and flatno columns. that way there will not be any duplicate combination of block_no and flatno and mysql throws error as soon as you try to insert a duplicate of this.
Alter table <tablename> add constraint UQ_FLATNO unique (block_no,flatno);
once this unique constraint is added then if you try to insert a duplicate flatno combination then the sql will throw a error.
In your query execution check can be done like this:
if (mysql_errno() == 1062) {
echo 'duplicate key';
}