【发布时间】: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 increment、user_id 和 contact_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.-> 如果您从一开始就没有时间正确地安全地 进行操作,那么您以后就找不到时间了!除非您必须从头开始重新创建它,因为有人删除了您的所有数据... 做或不做!没有尝试 -
这样想:如果你先用正确的方法做,你就不必回去做两次工作。另外,我们只是人类,事情很容易被遗忘。所以老实说,我看不出不马上做准备好的陈述的理由。