Something is wrong with my CardID in my INSERT Statement. Am I overlooking something that needs to be included? I am not getting an error, it just says there's an error on function bind_param()
I copy pasted the code from another page and my thought is that there are too many characters in the random cardid. The error did not appear until I tried to add the cardid to the insert.
Here's the table and the code.
// create the conditions for a random cardID
$CardIDLength = 20;
$CardIDString = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$CardIDDuplicate = true;
// make a random value for $cardID and then see if it exists in the DB
while ($CardIDDuplicate = true) {
$CardID = substr(str_shuffle($CardIDString), 0, $CardIDLength); //shuffle String, start with 0, 11 characters long
$CardIDSQL = "SELECT CardID FROM FlashcardTable WHERE CardID = ?";
$CardIDSTMT = $conn->prepare ($CardIDSQL);
$CardIDSTMT->bind_param("s", $CardID);
$CardIDSTMT->execute();
$CardIDRESULT = $CardIDSTMT->get_result();
$CardIDROW = $CardIDRESULT->fetch_assoc();
// Check table with search to find out if there are any values that match the one generated above
$CardIDRESULTCheck = mysqli_num_rows($CardIDRESULT);
if ($CardIDRESULTCheck <= 0) {
$CardIDDuplicate = false;
break;
}
else {
// repeat until you have a unique ID
}
}
$CardIDSTMT->free_result();
$CardIDSTMT->close();
$CardOrder = -1; // -1 = setname only, no cards created when the setname was initialized
$CardSQL = "INSERT INTO FlashcardTable (PostID, UserID, SetName, CardID, CardOrder, Visibility, Date) VALUES (?, ?, ?, ?, ?, ?)";
$CardSTMT = $conn->prepare ($CardSQL);
$CardSTMT->bind_param("ssssiss", $PostID, $UserID, $SetName, $CardID, $CardOrder, $Visibility, $Date);
$CardSTMT->execute();
$CardSTMT->free_result();
$CardSTMT->close();