I have this PHP code that allows me to know if I have registered friends (when I enter their profile, there is the button that says ADD), if the user is my friend it says ADDED. Very good. But I would like you to help me when I press the button (which is a link), it executes some code to insert the ID of this user in the DB (if it is not my friend). Whether through ajax, I don't know. But of course when I click.
I would like it to be within the ELSE.
<?php
include_once('conexion.php');
if ($user != $my_id) {
$mysqli = mysqli_connect("localhost", "root", "", "registros");
$quer = mysqli_query($mysqli,"SELECT * FROM friends WHERE (User_one =
'$my_id'
AND User_two = '$user') OR (User_two = '$user' AND User_one = '$my_id')");
if (mysqli_num_rows($quer) == 1)
{
$mensaje = "<a href= '#'>Already Friends</a>";
}
else{
$mensaje = "<a href= 'agregarN.php?Id=".$user."'>No Friends</a>";
}
}
?>
I would put this as a comment, since it is not part of the answer, but note it in the WHERE of your query
both conditions are the same, i think you meant to put this
As for your problem, I can think of this as a solution. Change the else to something like this:
The point is to create the id tag, and register the value of the friend you want to insert, if the id is 35, the tag would be `id="id35" and the class field will be used to call the ajax event .
Then by Ajax, you could do the insert
Assuming you have a link like this:
Just make a simple AJAX request:
And in the backend:
The way would be with ajax.
JS
PHP SAVE