I have this code (which worked):
if (isset($_POST['plant_name']) && $_POST['plant_name']) {
$where .= "AND (common_name) LIKE '".strtolower($_POST['plant_name']) . "' OR (latin_name) LIKE '".strtolower($_POST['plant_name'])."%' ";
}
But I wanted to change it to prepared statements and my attempt is below but I am getting errors:
$plant_name = $_POST['plant_name'];
if (isset($_POST['plant_name']) && $_POST['plant_name']) {
$stmt = $conn2->prepare . $where .= "AND (common_name) LIKE '".'?'. "' OR (latin_name) LIKE '".'?'."%' ";
}
$stmt->bind_param('s', $plant_name);
$stmt->execute();
Could somebody please help me out please
My errors are:
Notice: Undefined property: mysqli::$prepare
Fatal error: Call to a member function bind_param() on a non-object
EDIT: You’re using mysqli not PDO my fault. Unfortunately mysqli doesn’t support named parameters.
In your original example, you’re treating
$conn2->preparelike it’s a property, but it’s a function.Try this:
Here’s the PDO way (I think it’s a lot cleaner, but it’s probably not worth changing from mysqli to PDO at this point for you):
Note a few things about this:
?(numerically indexed placeholders) to:name(name-based placeholders). Since you’re using the same value for searching both fields, this gets you a very small performance gain, and makes the SQL a lot more readable.