I'm trying to insert data into one of my tables with AJAX.
This is my PHP Code:
include '../data/header.php'; //Here I get my DB-Connection Data from (username etc.)
$usernameToAdd = $_POST["usernameToAdd"];
$groupId = $_POST["groupId"];
$conn = new mysqli($dbserver, $dbuser, $dbpassword, $dbname);
$stmt = $conn->prepare("SELECT id FROM user WHERE username=?;");
$stmt->bind_param("s", $usernameToAdd);
$stmt->execute();
$stmt->bind_result($id);
if($stmt->fetch() > 0) {
$stmtInsert = $conn->prepare("INSERT INTO group_membership (userId, groupId) VALUES (?, ?);");
$stmtInsert->bind_param("ii", intval($id), intval($groupId));
$stmtInsert->execute();
}
It's a group-relation table. A userId get's added to a groupId.
And here is the data that gets posted:
groupId 29
usernameToAdd user1
or as raw data
groupId=29&usernameToAdd=user1
I'm wondering where I made a mistake? The User "user1" definitely exists in the table user.
Also here is my JavaScript Code if you need that:
function addUserToGroup(btn){
var groupId = btn.getAttribute("id").substring(13);
var usernameToAdd = $("#addToGroup"+groupId).val();
$.post("groups/addUserToGroup.php",
{
groupId: groupId,
usernameToAdd: usernameToAdd
},
function (data, status) {
});
}
It's always giving me a
Fatal error: Call to a member function bind_param() on boolean in D:\xampp\htdocs\myCalendar\groups\addUserToGroup.php on line 14
on this line $stmtInsert->bind_param("ii", intval($id), intval($groupId));
I don't know why and I've been trying to figure out for some time now.