【问题标题】:Why is my mysqli_fetch_assoc not grabbing the row info so I can insert details into my table?为什么我的 mysqli_fetch_assoc 没有获取行信息以便我可以将详细信息插入到我的表中?
【发布时间】:2017-04-29 08:30:02
【问题描述】:

首先,我知道 sql 注入,并且我的代码不是万无一失的,容易注入等。接下来会继续努力。

现在:从我的 Android 应用程序到我的 PHP 文件,我提交了一个电话号码的 JSON 数组,例如:

[{"phone_number":"+12345678"},
 {"phone_number":"+23456789"},
 {"phone_number":"34567890"},
 {"phone_number":"45678901"} 
 etc... etc...

这些是我的应用用户手机中的联系人。如果这些联系人也是我应用的用户,那么我想将这些号码插入我的contacts 表中。

但我无法让它工作。 mysqli_fetch_assoc 工作不正常。我不知道为什么。

在我的 contacts 表中,我有 3 列 - auto incrementuser_idcontact_id。前两个值插入正确,但contact_id 始终输入为“0”,这是错误的。

这是我的代码:

require('dbConnect.php');

//this is me, +567890123, my user_id in the user table
$user_id = '20';

//post all contacts in my phone as a JSON array
$json  = $_POST['phonenumber'];
$array = json_decode($json);

foreach ($array as $value) {
    $phonenumber = $value->phone_number;

    $sql      = "SELECT username FROM user WHERE username = '$phonenumber'";
    $result   = mysqli_query($con, $sql);
    $num_rows = mysqli_num_rows($result);

    if ($num_rows > 0) {
        echo "phonenumber is " . $phonenumber . "<br>";

        // we want to put $phonenumber in the contacts table, as one of +567890123 contacts
        // In the user table get the associated rows of $phonenumber
        while ($row = mysqli_fetch_assoc($result)) {
        // get the associated user_id in that row, that's what we want to put into the contacts table
            $contact_id                   = $row['user_id'];
            $insert_into_contacts_command = "INSERT INTO contacts VALUES(NULL, '$user_id','$contact_id')";
            $insert_into_contacts_table   = mysqli_query($con, $insert_into_contacts_command);
        }

    } //if +353864677745 is NOT in the user table...
    else {

        echo 'not a match.';
    }
}

【问题讨论】:

  • 而“不起作用”究竟是什么意思?请记住,我们不能越过你的肩膀。您需要准确地告诉我们发生了什么,您看到了什么,您的错误日志文件包含什么。
  • 除此之外,请注意您的代码很容易受到 sql 注入攻击。您应该了解将“准备好的语句”与“参数绑定”结合使用的好处,以使您的代码在该细节上更加健壮。
  • 请在您的页面顶部使用error_reporting(E_ALL); ini_set('display_errors', 1);,让我们知道 PHP 返回的错误内容
  • Will be working on that next. -> 如果您从一开始就没有时间正确地安全地 进行操作,那么您以后就找不到时间了!除非您必须从头开始重新创建它,因为有人删除了您的所有数据... 做或不做!没有尝试
  • 这样想:如果你先用正确的方法做,你就不必回去做两次工作。另外,我们只是人类,事情很容易被遗忘。所以老实说,我看不出不马上做准备好的陈述的理由。

标签: php mysqli


【解决方案1】:
$contact_id = $row['user_id'];

这里$contact_id 将是null,因为您正在尝试访问$row 中不存在的字段$row['user_id']

实际上,您的结果集中只有一个字段username,正如您指定的那样:

$sql = "SELECT username FROM user WHERE username = '$phonenumber'";

尝试将您的查询更改为:

$sql = "SELECT user_id, username FROM user WHERE username = '$phonenumber'";

【讨论】:

    【解决方案2】:

    您的查询选择列username,而不是userid

    你还没有发布任何关于表 user 的信息,所以很难建议一个新的查询,但我猜它是以下内容:

    $stmt = mysqli_prepare($con, "SELECT userid FROM user WHERE username = ?");
    $stmt->bind_param("s", $phonenumber);
    $stmt->execute();
    $stmt->bind_result($userid);
    
    while ($stmt->fetch()) {
        // Work with $userid
    }
    

    您会注意到,这使用了带有绑定参数的预准备语句。这样,您的代码就不容易受到 SQL 注入的影响。

    【讨论】:

    • 好发现!然而,这重复了使代码易受攻击的主要打击,这在答案中不是是一件好事。 至少你应该清楚地指出这个问题。最好提供一个问题较少的例子。
    • @arkascha 没有不尊重,但无论我的代码是否易受攻击,它都是有效的 PHP 代码,它会出现问题并要求答案,这是 stackoverflow 的目的 - 人们有代码问题,人们回答他们。也许另一个必须添加另一层的站点 - 问题必须基于代码并且代码必须完全安全才能获得答案,我不知道。
    • @CHarris:不,arkascha 向我提及了这一点,而不是向您提及,并且这句话是正确的。毕竟我想写好代码。
    • @Pharaoh 我的 $insert_into_contacts_command 需要保护还是可以?
    • 一切都需要保护 :) 当然,从代码外部接触输入的一切(如$_POST)都需要防水,但即使是不这样做的代码也会使您容易受到攻击。只需保护所有内容 - 您编写查询的速度将比您考虑是否保护此特定查询的速度更快。去做吧。
    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2019-12-10
    • 2016-07-11
    • 2013-04-09
    相关资源
    最近更新 更多