我有一个用户表,每个用户都有一个字段“friends”。在该字段中,我想保存一个包含朋友用户ID的数组(例如:“189”,“30”,“48”)。
通过角度前端我用url.com/api/?friend=48&user_id=6发送POST请求
但我创建的api代码不起作用,我做错了什么?
// Create connection
$con=mysqli_connect("localhost","...","...","..");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = mysql_query("SELECT * FROM api_users WHERE id='$_POST[user_id]'");
// get current value
$row = mysql_fetch_assoc($query);
$friends = array($row['friends']);
// add nieuw friend to array
$newfriend = $_POST['friend'];
$addfriendlist = array_push($friends,$newfriend);
// update the array with the new friend to the database
$sql="UPDATE api_users SET friends="$addfriendlist" WHERE id='$_POST[user_id]'";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
}