检查数据库中是否已存在变量数据

时间:2013-11-24 23:50:38

标签: php mysql duplicates

我正在尝试按照之前回答的问题How to check if value already exists in MySQL database

这是我的代码

<?php
$con=mysql_connect(Localhost,"mwtwoone_xbl","password","mwtwoone_xbl");
mysql_select_db( 'mwtwoone_xbl' );

$txn_id = $_GET['txn'];

echo $txn_id;

$checktxnID = mysql_query("SELECT txn_id from codes WHERE txn_id = '$txn_id'");

if (!$checktxnID) {
    die('Query failed to execute for some reason');
}

if (mysql_num_rows($checktxnId) > 0) {
echo "User id exists already.";
$user = mysql_fetch_array($checktxnId);
print_r($user); // the data returned from the query
}

echo "User id doesnt exist";

mysql_close($con);
?>

$ txn_id将被定义,我只需要检查它的值是否已经存在于列txn_id的表代码中。感谢所有的帮助,非常感谢。

现在我访问URL .php?txn = 123 我得到123User id已经不存在了。 问题是,表格代码columb txn_id上的多行包含值123!

2 个答案:

答案 0 :(得分:3)

您在条件$checktxnId支票中使用了$checktxnID而不是> 0

答案 1 :(得分:-2)

尝试更改此行

$checktxnID = mysql_query("SELECT txn_id from codes WHERE txn_id = '$txn_id'");

$checktxnID = mysql_query("SELECT txn_id from codes WHERE txn_id = '".$txn_id."'");
相关问题