【问题标题】:Checking if variable data already exist in databse检查数据库中是否已经存在变量数据
【发布时间】:2013-11-28 15:09:16
【问题描述】:

我正在尝试关注这个先前回答的问题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 我得到 123 用户 ID 不存在。 问题是,表代码columb txn_id 上的多行包含值123!

【问题讨论】:

  • 哦哦。我想首先说 mysql_* 函数已被弃用。 :)
  • 然后呢?有什么错误吗?有什么问题?
  • 啊,很抱歉,更新了原帖。
  • LIMIT 1 添加到您的查询中。
  • 请仔细阅读此内容,您很容易接受注射 => stackoverflow.com/q/60174/1415724

标签: php mysql duplicates


【解决方案1】:

您在条件&gt; 0check 中使用了$checktxnId 而不是$checktxnID

【讨论】:

  • 该死,我无法相信过去一个小时的沮丧是因为那个小错误,非常感谢。
  • 这就是为什么我从不从不使用任何混合字母大小写。错字错误的空间太大。我总是使用下划线作为分隔符。 @user3029524
  • @user3029524 我建议你总是打开错误报告。 error_report(E_ALL)。参考php.net/manual/en/function.error-reporting.php
  • @Fred-ii-camelCase 用于变量和 under_scores 用于数组键是基本的编码标准。他的问题是没有使用合适的 IDE:NetBeans(免费)、phpStorm(付费)...等等。
  • 你说得有道理。 @keyboardSmasher 我自己就是“老派”;-)
【解决方案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."'");

【讨论】:

  • 刚刚这样做,仍然收到相同的消息!
  • 使用双引号,所以变量值会被插值
  • 我应该切换双引号/单引号吗?
  • 否,单引号和连接或者双引号用于插值。 (所以请保持原样,尽管您正在注入用户输入,这当然很糟糕)
猜你喜欢
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2020-02-04
  • 2015-07-12
相关资源
最近更新 更多