dptpn06684 2017-04-29 08:30
浏览 65
已采纳

为什么我的mysqli_fetch_assoc没有抓取行信息,所以我可以在我的表中插入细节?

First off, I know about sql injection and that my code is not foolproof, prone to injection etc. Will be working on that next.

Now : from my Android app to my PHP file I submit a JSON array of phone numbers like :

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

These are contacts in my app user's phone. If these contacts are people who are also users of my app then I want to insert those numbers into my contacts table.

But I can't get it to work. mysqli_fetch_assoc isn't working correctly. I don't know why.

In my contacts table I have 3 columns - an auto increment, user_id and contact_id. The first two values are inserted correctly but the contact_id is always put in as '0', which is wrong.

Here is my code :

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.';
    }
}
  • 写回答

2条回答 默认 最新

  • doutongfu9484 2017-04-29 08:34
    关注

    Your query selects the column username, not userid.

    You haven't posted anything about the table user, so it's hard to suggest a new query, but I guess it's the following:

    $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
    }
    

    You'll note that this uses a prepared statement with a bound parameter. That way, your code is not prone to SQL injections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址