I'm familiar with the basics of how mysqli_stmt_bind_param works, but I'm still new to PHP and running into an issue I'm not sure how to tackle. How would I implement mysqli_stmt_bind_param method whenever I'm not sure how many ?'s will be in the SQL query? I've attached an example of a code below that illustrates what I'm talking about. I've also included a screenshot of the database structure, if that helps any.
In this example, the SQL statement is built depending on if arguments are present in the URL, other conditions, etc. There could potentially be anywhere from 1 ? to 10 ?'s, which I'm not sure how to navigate.
I want to accomplish this, because my understanding is that stmt_bind_param is safer than doing it the way I'm currently doing it.. right?
if (isset($_GET['type'])) {
if ($_GET['type'] == "forgiven") {$supplemental = "discipline.forgiven = 1 AND ";}
else if ($_GET['type'] == "active") {$supplemental = "discipline.forgiven = 0 AND ";}
else if ($_GET['type'] == "all") {$supplemental = "discipline.forgiven IS NOT NULL AND ";}
else {$supplemental = "discipline.type = '" .$_GET['type']. "' AND ";}
}
if (isset($_GET['uuid'])) {$SQL = "SELECT users.firstName, users.lastName, users.homeroom, discipline.* FROM `discipline` INNER JOIN users ON users.uuid = discipline.recipient WHERE " .$supplemental. "discipline.recipient LIKE '" .$_GET['uuid']. "' ORDER BY discipline.timestamp DESC";}
else {$SQL = "SELECT users.firstName, users.lastName, users.homeroom, discipline.* FROM `discipline` INNER JOIN users ON users.uuid = discipline.recipient WHERE " .$supplemental. "users.homeroom LIKE '" .$_GET['search']. "' OR discipline.recipient LIKE '" .$_GET['search']. "' OR discipline.reason LIKE '%" .$_GET['search']. "%' OR discipline.action LIKE '%" .$_GET['search']. "%' ORDER BY discipline.timestamp DESC";}
$stmt = mysqli_stmt_init($connection);
if (!mysqli_stmt_prepare($stmt, $SQL)) {echo "SQL database connection error!";exit();}
else {
mysqli_stmt_execute($stmt);
$results = mysqli_stmt_get_result($stmt);
Table Structure: